Thread: Returning pointer to a struct defined inside a class, multiple scope operators

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Returning pointer to a struct defined inside a class, multiple scope operators

    my header file for a basic binary tree includes the following:

    Code:
    template<typename T>
    class Btree{
        // OVERVIEW:    a binary tree with flexible structure that is not sorted
    
    private:
    
        struct node {            //a container object 
            node *left;        //left and right tree
            node *right;
            T *o;        // pointer to  object of node
        };
    
    public:
    
        node *root;    //pointer to the root of the tree (NULL if empty)
    
        node* insert (node *parent, T *child, int child);
        //MODIFIES:    this
        //EFFECTS:    creates a node that stores a pointer to the new child 
        //        and returns the pointer to the node of the new child    
        //        the integer child is either 0, for left child,
        //        or anything else for right child    
    
    //    void printTree (node * root);
        //EFFECTS:    takes the root of a tree and prints the tree's 
        //        coordinates  
        
        Btree();    //ctor
        Btree(){}    //dtor
    
    
    };
    
    #include "btree.cpp"
    -----------------------------------------------------------------------------------------

    my .cpp looks like this, and not that it is included at the bottom of my header to avoid template compiler errors:

    Code:
    template <typename T>
    Btree<T> :: node * Btree<T>::insert (node *parent, T *child, int child)
    
    {
        node *np = new node;
        np-> o = child;    
        np->left = NULL;
        np->right  = NULL;
        if (child == 0)
            parent->left = np;
        else
            parent->right = np;
        return np;
    }
    however, i get the following compiler error:
    btree.cpp:3: error: expected constructor, destructor, or type conversion before ‘*’ token

    i'm compiling on 4.1.2 of the g++ compiler

    Can anyone help!?!?! http://im.cprogramming.com/images/smilies/frown.png

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    > node* insert (node *parent, T *child, int child);
    Why does two of them has the same name ?

    >Btree<T> :: node * Btree<T>::insert (node *parent, T *child, int child)
    Apparently, a newer version of the compiler will you better error messages.
    Quote Originally Posted by g++
    a.cpp:34:1: error: need ‘typename’ before ‘Btree<T>::node’ because ‘Btree<T>’ is a dependent scope
    The error literally instructs you what to do (I had this a while ago.. and totally ignored it thinking it meant something else!..don't do that.)

    Also.. this can't be done:
    Code:
        Btree();    //ctor
        Btree(){}    //dtor
    Last edited by manasij7479; 12-04-2011 at 06:13 PM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    THANK YOU! so helpful!

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    when I removed all the templates I am getting errors again. I followed what others recommend and my code looks like this:

    Code:
    class Btree{
        // OVERVIEW:    a binary tree that is not sorted
    
    private:
    
        struct node {            //a container object 
            node *left;        //if the direction is straight
            node *right;        //if a right turn is made
            node *parent;        // points to the parent
            tile *t;        // pointer to  object of node
        };
    
    public:
    
        node *root;    //pointer to the root of the tree (NULL if empty)
    
        node * insert (tile *parent, tile *child, int childNum);
        //MODIFIES:    this
        //EFFECTS:    creates a node that stores a pointer to the new child 
        //        and returns the pointer to the node of the new child    
        //        the integer child is either 0, for left child,
        //        or anything else for right child
        //        for the purposes of this project, all right turns
        //        are stored on the right subtree
    
        tile * removeAll(node *root);
        //EFFECTS:    deletes all nodes from the tree. called in dtor
        
        Btree();    //ctor
        Btree(tile *o);    //alternative ctor, sets root to o
        ~Btree();    //dtor
    
    
    };
    and the .cpp file has this now

    Code:
    Btree :: node * Btree ::insert(node *parent, tile *child, int childNum)
    {
        node *np = new node;
        np-> t = child;    
        np->left = NULL;
        np->right  = NULL;
        if (root == NULL){
            root = np;
        } else{
            np->parent = parent;
            if (childNum == 0)
                parent->left = np;
            else
                parent->right = np;
        }
        return np;
    }
    with this compiler error:

    btree.cpp:3: error: prototype for ‘Btree::node* Btree::insert(Btree::node*, tile*, int)’ does not match any in class ‘Btree’
    btree.h:31: error: candidate is: Btree::node* Btree::insert(tile*, tile*, int)
    any ideas?!

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Read the error message. It says that your function prototype in line 3 of btree.cpp does not match the prototype in line 31 of the header.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. Replies: 2
    Last Post: 12-06-2010, 01:28 PM
  3. struct pointer problem (out of scope?)
    By Axpen in forum C Programming
    Replies: 4
    Last Post: 05-17-2005, 09:48 AM
  4. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM
  5. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 11:04 PM

Tags for this Thread