so add the name of the cpp++ implementation file to the header? O_O
the one I just posted is the implementation file that defines the header functions, not the header itself.
so add the name of the test function to the implementation file? :O
Printable View
so add the name of the cpp++ implementation file to the header? O_O
the one I just posted is the implementation file that defines the header functions, not the header itself.
so add the name of the test function to the implementation file? :O
K, did it, it says it can't open "question1test.cpp" file (the name of my file), and that it's an invalid argument.
I don't think you're doing what I told you to. I said to include this file:
at the end of *this* file:Code:#include<iostream>
#include"tree.h"
using namespace std;
template<class elemType>
bool binaryTreeType<elemType>::isEmpty()
{
return(root==NULL);
}
...etc
Code:template<class elemType>
struct nodeType
{
int key_value;
nodeType<elemType> *left;
nodeType<elemType> *right;
elemType info;
nodeType<elemType> *llink;
nodeType<elemType> *rlink;
};
template<class elemType>
class binaryTreeType
...etc
I did that, it just gives a whole bunch of errors saying that the class template has already been defined.
this is what the header looks like now:
the implementationPHP Code:template<class elemType>
struct nodeType
{
int key_value;
nodeType<elemType> *left;
nodeType<elemType> *right;
elemType info;
nodeType<elemType> *llink;
nodeType<elemType> *rlink;
};
template<class elemType>
class binaryTreeType
{
public:
const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&);
bool isEmpty();
void inorderTraversal();
void preorderTraversal();
void postorderTraversal();
int treeHeight();
int treeNodeCount();
int treeLeavesCount();
void destroyTree();
int nodeCount(nodeType<elemType> *p);
int leavesCount(nodeType<elemType> *p);
binaryTreeType(const binaryTreeType<elemType>& otherTree);
binaryTreeType();
~binaryTreeType();
protected:
nodeType<elemType> *root;
private:
void copyTree(nodeType<elemType>* &copiedTreeRoot, nodeType<elemType>* otherTreeRoot);
void destroy(nodeType<elemType>* &p);
void inorder(nodeType<elemType> *P);
void preorder(nodeType<elemType> *p);
void postorder(nodeType<elemType> *p);
int height(nodeType<elemType> *p);
int max (int x, int y);
};
#include"question1imp.cpp"
PHP Code:#include<iostream>
#include"tree.h"
using namespace std;
template<class elemType>
bool binaryTreeType<elemType>::isEmpty()
{
return(root==NULL);
}
template<class elemType>
binaryTreeType<elemType>::binaryTreeType()
{
root =NULL;
}
template<class elemType>
void binaryTreeType<elemType>::inorderTraversal()
{
inorder(root);
}
template<class elemType>
void binaryTreeType<elemType>::preorderTraversal()
{
preorder(root);
}
template<class elemType>
void binaryTreeType<elemType>::postorderTraversal()
{
postorder(root);
}
template<class elemType>
int binaryTreeType<elemType>::treeHeight()
{
return height(root);
}
template<class elemType>
int binaryTreeType<elemType>::treeNodeCount()
{
return nodeCount(root);
}
template<class elemType>
int binaryTreeType<elemType>::treeLeavesCount()
{
return leavesCount(root);
}
template<class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p)
{
if(p !=NULL)
{
inorder(p->llink);
cout<<p-=>info<<" ";
inorder(p->rlink);
}
}
template<class elemType>
void binaryTreeType<elemType>::preorder(nodeType<elemType> *p)
{
if (p !=NULL)
{
cout<<p->info<<" ";
preorder(p->llink);
preorder(p->rlink);
}
}
template<class elemType>
void binaryTreeType<elemType>::postorder(nodeType<elemType> *p)
{
if (p !=NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<<p->info<<" ";
}
}
template<class elemType>
int binaryTreeType<elemType>::height(nodeType<elemType> *p)
{
if (p==NULL)
return 0;
else
return 1 + max(height(p->llink),height(p->rlink));
}
template<class elemType>
int binaryTreeType<elemType>::max(int x, int y)
{
if (x>=y)
return x;
else
return y;
}
template<class elemType>
int binaryTreeType<elemType>:: nodeCount(nodeType<elemType> *p)
{
if (p==NULL)
return 0;
else{
int count=1;
count+=nodeCount(p->llink);
count+=nodeCount(p->rlink);
return count;
}
}
template<class elemType>
int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p)
{
if (p->llink==NULL && p->rlink==NULL)
return 1;
else
return leavesCount(p->llink)+leavesCount(p->rlink);
}
template<class elemType>
void binaryTreeType<elemType>::copyTree(nodeType<elemType>* &copiedTreeRoot, nodeType<elemType>* otherTreeRoot)
{
if (otherTreeRoot ==NULL)
copiedTreeRoot =NULL;
else
{
copiedTreeRoot=new nodeType<elemType>;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
}
}
template<class elemType>
void binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
{
if (p !=NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p=NULL;
}
}
template<class elemType>
binaryTreeType<elemType>::binaryTreeType(const binaryTreeType<elemType>& otherTree)
{
if (otherTree.root==NULL)
root=NULL;
else
copyTree(root, otherTree.root);
}
template<class elemType>
binaryTreeType<elemType>::~binaryTreeType()
{
destroy(root);
}
template<class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::operator=(const binaryTreeType<elemType>& otherTree)
{
if (this != &otherTree)
{
if (root != NULL)
destroy(root);
if (otherTree.root ==NULL)
root=NULL;
else
copyTree(root, otherTree.root);
}
return *this;
}
and the test file:
PHP Code:
#include<iostream>
#include "tree.h"
using namespace std;
int main()
{
nodeType<int> *root;
nodeType<int> *buddy;
binaryTreeType<int> test;
root=new nodeType<int>;
root->key_value=20;
root->left=NULL;
root->right=NULL;
buddy=root;
buddy->left=new nodeType<int>;
buddy->left->key_value=5;
buddy->left->left=NULL;
buddy->left->right=NULL;
buddy->right=new nodeType<int>;
buddy->right->key_value=5;
buddy->right->left=NULL;
buddy->right->right=NULL;
buddy=root;
int c= test.nodeCount(buddy);
cout<<"The are "<<c<<" nodes in this tree"<<endl;
delete root;
delete buddy;
return 0;
}
That is because you dont have proper include guards.....
EVERY header file should have:
Code:#ifndef SOME_UNIQUE_VALUE_REPRESENTING_FILE
#define SOME_UNIQUE_VALUE_REPRESENTING_FILE
...
...
...
#endif
It works! holy crap!
now I have only one issue, and that's the whole defining an object using a template class.
When I run the program I get an error, probably because I'm creating the object incorrectly and calling the function incorrectly as well.
is binaryTreeType<int> *variable name;
the right way to define an object for this class?
if so, why do I receive an error when I run the program.
I can't thank you guys enough, if I can understand this small problem, I'm sure I can make my own template programs confidently. :)
Well, that's certainly how you'd define a pointer to an object of that class.
okay, so that adds to confusion, I tried working everything through the class functions, and I still get an error.
This is what I have now:
Test function
implementation fileCode:#include<iostream>
#include "tree.h"
using namespace std;
int main()
{
binaryTreeType<int> test;
test.makeTree();
cout<<"The are "<<test.treeNodeCount()<<" nodes in this tree"<<endl;
return 0;
}
header fileCode:#include<iostream>
#include"tree.h"
using namespace std;
template<class elemType>
bool binaryTreeType<elemType>::isEmpty()
{
return(root==NULL);
}
template<class elemType>
binaryTreeType<elemType>::binaryTreeType()
{
root =NULL;
}
template<class elemType>
void binaryTreeType<elemType>::inorderTraversal()
{
inorder(root);
}
template<class elemType>
void binaryTreeType<elemType>::preorderTraversal()
{
preorder(root);
}
template<class elemType>
void binaryTreeType<elemType>::postorderTraversal()
{
postorder(root);
}
template<class elemType>
int binaryTreeType<elemType>::treeHeight()
{
return height(root);
}
template<class elemType>
int binaryTreeType<elemType>::treeNodeCount()
{
return nodeCount(buddy);
}
template<class elemType>
int binaryTreeType<elemType>::treeLeavesCount()
{
return leavesCount(root);
}
template<class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p)
{
if(p !=NULL)
{
inorder(p->llink);
cout<<p-=>info<<" ";
inorder(p->rlink);
}
}
template<class elemType>
void binaryTreeType<elemType>::preorder(nodeType<elemType> *p)
{
if (p !=NULL)
{
cout<<p->info<<" ";
preorder(p->llink);
preorder(p->rlink);
}
}
template<class elemType>
void binaryTreeType<elemType>::postorder(nodeType<elemType> *p)
{
if (p !=NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<<p->info<<" ";
}
}
template<class elemType>
int binaryTreeType<elemType>::height(nodeType<elemType> *p)
{
if (p==NULL)
return 0;
else
return 1 + max(height(p->llink),height(p->rlink));
}
template<class elemType>
int binaryTreeType<elemType>::max(int x, int y)
{
if (x>=y)
return x;
else
return y;
}
template<class elemType>
int binaryTreeType<elemType>:: nodeCount(nodeType<elemType> *p)
{
if (p==NULL)
return 0;
else{
int count=1;
count+=nodeCount(p->llink);
count+=nodeCount(p->rlink);
return count;
}
}
template<class elemType>
int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p)
{
if (p->llink==NULL && p->rlink==NULL)
return 1;
else
return leavesCount(p->llink)+leavesCount(p->rlink);
}
template<class elemType>
void binaryTreeType<elemType>::copyTree(nodeType<elemType>* &copiedTreeRoot, nodeType<elemType>* otherTreeRoot)
{
if (otherTreeRoot ==NULL)
copiedTreeRoot =NULL;
else
{
copiedTreeRoot=new nodeType<elemType>;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
}
}
template<class elemType>
void binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
{
if (p !=NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p=NULL;
}
}
template<class elemType>
void binaryTreeType<elemType>::makeTree()
{
nodeType<int> *buddy;
root=new nodeType<int>;
root->key_value=20;
root->left=NULL;
root->right=NULL;
buddy=root;
buddy->left=new nodeType<int>;
buddy->left->key_value=5;
buddy->left->left=NULL;
buddy->left->right=NULL;
buddy->right=new nodeType<int>;
buddy->right->key_value=5;
buddy->right->left=NULL;
buddy->right->right=NULL;
buddy=root;
}
template<class elemType>
binaryTreeType<elemType>::binaryTreeType(const binaryTreeType<elemType>& otherTree)
{
if (otherTree.root==NULL)
root=NULL;
else
copyTree(root, otherTree.root);
}
template<class elemType>
binaryTreeType<elemType>::~binaryTreeType()
{
destroy(root);
}
template<class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::operator=(const binaryTreeType<elemType>& otherTree)
{
if (this != &otherTree)
{
if (root != NULL)
destroy(root);
if (otherTree.root ==NULL)
root=NULL;
else
copyTree(root, otherTree.root);
}
return *this;
}
The problems in the implementation, but I don't see why.Code:#ifndef TREE_H
#define TREE_H
template<class elemType>
struct nodeType
{
int key_value;
nodeType<elemType> *left;
nodeType<elemType> *right;
elemType info;
nodeType<elemType> *llink;
nodeType<elemType> *rlink;
};
template<class elemType>
class binaryTreeType
{
public:
const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&);
bool isEmpty();
void inorderTraversal();
void preorderTraversal();
void postorderTraversal();
int treeHeight();
int treeNodeCount();
int treeLeavesCount();
void destroyTree();
int nodeCount(nodeType<elemType> *p);
void makeTree();
int leavesCount(nodeType<elemType> *p);
binaryTreeType(const binaryTreeType<elemType>& otherTree);
binaryTreeType();
~binaryTreeType();
protected:
nodeType<elemType> *root;
nodeType<elemType> *buddy;
private:
void copyTree(nodeType<elemType>* &copiedTreeRoot, nodeType<elemType>* otherTreeRoot);
void destroy(nodeType<elemType>* &p);
void inorder(nodeType<elemType> *P);
void preorder(nodeType<elemType> *p);
void postorder(nodeType<elemType> *p);
int height(nodeType<elemType> *p);
int max (int x, int y);
};
#include"question1imp.cpp"
#endif
or maybe it's how I try to cout the number?
How wonderfully vague. Perhaps I can offer you "a solution". It involves doing stuff with things.Quote:
an error
Seriously, details.
it says the problem occurs here during the program execution:
Code:template<class elemType>
int binaryTreeType<elemType>:: nodeCount(nodeType<elemType> *p)
{
if (p==NULL)
return 0;
else{
int count=1;
count=count+nodeCount(p->llink);
count=count+nodeCount(p->rlink);
return count;
}
}
It's to count the number of nodes in the left leaf, then the number of nodes in the right leaf.
It is the debugger, and it says this:
auto p:
key_value CXX0030: Error: expression cannot be evaluated
left CXX0030: Error: expression cannot be evaluated
right CXX0030: Error: expression cannot be evaluated
info CXX0030: Error: expression cannot be evaluated
llink CXX0030: Error: expression cannot be evaluated
rlink CXX0030: Error: expression cannot be evaluated
so apparently p is not getting values.... >_>
but it should be.
OK, I missed that.
Forget about the debugger for now. Does the program run correctly without the debugger?Quote:
It is the debugger, and it says this:
All of those messages just mean that the debugger can't display those values for whatever reason (they are not in scope, they've been optimized away, whatever). It shouldn't be used to verify if your program is working correctly.
Regards,
Paul McKenzie
Yeah, it's the .exe that crashes.
and it says it's for the reason that *p, can't be defined. Yet I made a tree. :/