0

How can I get one of my functions to return a pointer to a struct? I would like my function called insertHelper to return a pointer to a Node struct. I have tried to implement this function header as:

Node* insertHelper(Node *rootPointer, Node *nodePointer); but when I try and compile, I get an error and am told that Node does not name a type. I have the Node struct defined in my .h file.

How can I get the above function to return a pointer to a Node?

Here is my .h file:

#ifndef Bintree_H                                 
#define Bintree_H
#include "nodedata.h"

using namespace std;

class BinTree{          // you add class/method comments and assumptions

    friend ostream& operator<<(ostream& out, const BinTree& T);  //Used for output printing of BinTree objects

    public:
        BinTree();                              // constructor
        BinTree(const BinTree &);               // copy constructor
        ~BinTree();                             // destructor, calls makeEmpty  
        bool isEmpty() const;                   // true if tree is empty, otherwise false
        void makeEmpty();                       // make the tree empty so isEmpty returns true
        BinTree& operator=(const BinTree &);
        bool operator==(const BinTree &) const;
        bool operator!=(const BinTree &) const;
        bool insert(NodeData*);
        bool retrieve(const NodeData &, NodeData* &) const; //I got these parameters from the PDF.
        void displaySideways() const;           // provided below, displays the tree sideways
        int getHeight(const NodeData &) const;
        void bstreeToArray(NodeData* []);
        void arrayToBSTree(NodeData* []);
        
    private:
        struct Node {
            NodeData* data;                     // pointer to data object
            Node* left;                         // left subtree pointer
            Node* right;                        // right subtree pointer
        };
        Node* root;                             // root of the tree

        //Utility functions
        void sideways(Node*, int) const; //provided below, helper for displaySideways()
        //bool insertHelper(Node *rootPointer, Node *nodePointer);
        Node* insertHelper(Node *rootPointer, Node *nodePointer);
        void postOrderDeleteNode(const Node *node);
        void inorderHelper(Node *startNode) const;

};

#endif

Here is the location in my .cpp file where I am trying to implement the function(ignore all the print statements that I use for debug):

Node* BinTree::insertHelper(Node *rootPointer, Node *nodePointer){
    cout << "insertHelper function" << endl;
    cout << "rootPointer: " << rootPointer << endl;
    cout << "nodePointer: " << nodePointer << endl;

    if(rootPointer==nullptr){
        cout << "weve reached a base case in the insertHelper" << endl;
        rootPointer = nodePointer; 
        
    }
    else{
        if(nodePointer->data <= root->data){
            cout << "if statement" << endl;
            insertHelper(root->left,nodePointer);
        }
        else if(nodePointer->data >= root->data){ //we go here if the NodeData being added is larger than the current node.
            cout << "else if statement" << endl;
            insertHelper(root->right, nodePointer);
        }
    }
}

  • 1
    Do you get the error on the declaration of the definition? Please provide a [mre] with the full error message – Alan Birtles Jan 23 '21 at 06:49
  • Hi @Alan Birtles, this is the specific error ```.\bintree.cpp:122:1: error: 'Node' does not name a type 122 | Node* BinTree::insertHelper(Node *rootPointer, Node *nodePointer){``` So I suppose that is the definition? – Aviv Weinstein Jan 23 '21 at 06:52
  • Please edit any additional details into the question and add a [mre], the error is in your cpp file not the header, we need to see that – Alan Birtles Jan 23 '21 at 06:52
  • Hi @AlanBirtles, I added in the function from my .cpp implementation file. – Aviv Weinstein Jan 23 '21 at 06:56
  • 1
    In the cpp file, since `Node` is a member of `BinTree` you need to qualify `Node`, like `BinTree::Node`. – jkb Jan 23 '21 at 06:59
  • Unrelated but see also https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers#:~:text=You%20should%20definitely%20NOT%20use,another%20reason%20it's%20so%20dangerous. – Alan Birtles Jan 23 '21 at 07:20

0 Answers0