|
-
November 8th, 2008, 07:23 PM
#16
Re: Transferring Structure variables?(simple problem)
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
-
November 8th, 2008, 08:19 PM
#17
Re: Transferring Structure variables?(simple problem)
 Originally Posted by Lindley
Try adding
Code:
#include "tree.cpp"
to the very end of tree.h.
I don't think these instructions are in any way unclear. If it works, we can try to explain why in more detail. If it doesn't we'll deal with that then.
-
November 8th, 2008, 09:29 PM
#18
Re: Transferring Structure variables?(simple problem)
K, did it, it says it can't open "question1test.cpp" file (the name of my file), and that it's an invalid argument.
-
November 8th, 2008, 11:24 PM
#19
Re: Transferring Structure variables?(simple problem)
I don't think you're doing what I told you to. I said to include this file:
Code:
#include<iostream>
#include"tree.h"
using namespace std;
template<class elemType>
bool binaryTreeType<elemType>::isEmpty()
{
return(root==NULL);
}
...etc
at the end of *this* file:
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
-
November 8th, 2008, 11:40 PM
#20
Re: Transferring Structure variables?(simple problem)
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:
PHP 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"
the implementation
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;
}
-
November 9th, 2008, 12:36 PM
#21
Re: Transferring Structure variables?(simple problem)
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
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 9th, 2008, 06:58 PM
#22
Re: Transferring Structure variables?(simple problem)
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.
-
November 9th, 2008, 07:11 PM
#23
Re: Transferring Structure variables?(simple problem)
Well, that's certainly how you'd define a pointer to an object of that class.
-
November 9th, 2008, 07:24 PM
#24
Re: Transferring Structure variables?(simple problem)
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
Code:
#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;
}
implementation file
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(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;
}
header file
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
The problems in the implementation, but I don't see why.
or maybe it's how I try to cout the number?
-
November 9th, 2008, 08:36 PM
#25
Re: Transferring Structure variables?(simple problem)
How wonderfully vague. Perhaps I can offer you "a solution". It involves doing stuff with things.
Seriously, details.
-
November 10th, 2008, 12:34 AM
#26
Re: Transferring Structure variables?(simple problem)
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;
}
}
-
November 10th, 2008, 04:46 AM
#27
Re: Transferring Structure variables?(simple problem)
 Originally Posted by pyromonki
it says
What is "it", and what does "it" exactly "say"?
Code:
count=count+nodeCount(p->llink);
count=count+nodeCount(p->rlink);
Why is this done twice??
Regards,
Paul McKenzie
-
November 10th, 2008, 08:00 PM
#28
Re: Transferring Structure variables?(simple problem)
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.
-
November 10th, 2008, 08:04 PM
#29
Re: Transferring Structure variables?(simple problem)
 Originally Posted by pyromonki
It's to count the number of nodes in the left leaf, then the number of nodes in the right leaf.
OK, I missed that.
It is the debugger, and it says this:
Forget about the debugger for now. Does the program run correctly without the debugger?
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
-
November 10th, 2008, 09:03 PM
#30
Re: Transferring Structure variables?(simple problem)
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. :/
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|