CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 34
  1. #16
    Join Date
    Nov 2008
    Posts
    15

    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

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Transferring Structure variables?(simple problem)

    Quote Originally Posted by Lindley View Post
    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.

  3. #18
    Join Date
    Nov 2008
    Posts
    15

    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.

  4. #19
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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

  5. #20
    Join Date
    Nov 2008
    Posts
    15

    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>* &copiedTreeRootnodeType<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 xint 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(
    !=NULL)
        {
            
    inorder(p->llink);
            
    cout<<p-=>info<<" ";
            
    inorder(p->rlink);
        }
    }
    template<class elemType>
    void binaryTreeType<elemType>::preorder(nodeType<elemType> *p)
    {
        if (
    !=NULL)
        {
            
    cout<<p->info<<" ";
            
    preorder(p->llink);
            
    preorder(p->rlink);
        }
    }
    template<class elemType>
    void binaryTreeType<elemType>::postorder(nodeType<elemType> *p)
    {
        if (
    !=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 
    max(height(p->llink),height(p->rlink));
    }
    template<class elemType>
    int binaryTreeType<elemType>::max(int xint 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>* &copiedTreeRootnodeType<elemType>* otherTreeRoot)
    {
        if (
    otherTreeRoot ==NULL)
            
    copiedTreeRoot =NULL;
        else
        {
            
    copiedTreeRoot=new nodeType<elemType>;
            
    copiedTreeRoot->info otherTreeRoot->info;
            
    copyTree(copiedTreeRoot->llinkotherTreeRoot->llink);
            
    copyTree(copiedTreeRoot->rlinkotherTreeRoot->rlink);
        }
    }

    template<class elemType>
    void binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
    {
        if (
    !=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(roototherTree.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(roototherTree.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<inttest;


        
    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 ctest.nodeCount(buddy);

        
    cout<<"The are "<<c<<" nodes in this tree"<<endl;

        
    delete root;
        
    delete buddy;
        return 
    0;


  6. #21
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  7. #22
    Join Date
    Nov 2008
    Posts
    15

    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.

  8. #23
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Transferring Structure variables?(simple problem)

    Well, that's certainly how you'd define a pointer to an object of that class.

  9. #24
    Join Date
    Nov 2008
    Posts
    15

    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?

  10. #25
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Transferring Structure variables?(simple problem)

    an error
    How wonderfully vague. Perhaps I can offer you "a solution". It involves doing stuff with things.

    Seriously, details.

  11. #26
    Join Date
    Nov 2008
    Posts
    15

    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;
    	}
    }

  12. #27
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Transferring Structure variables?(simple problem)

    Quote Originally Posted by pyromonki View Post
    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

  13. #28
    Join Date
    Nov 2008
    Posts
    15

    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.

  14. #29
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Transferring Structure variables?(simple problem)

    Quote Originally Posted by pyromonki View Post
    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

  15. #30
    Join Date
    Nov 2008
    Posts
    15

    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. :/

Page 2 of 3 FirstFirst 123 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured