Reputation: 3
I get this error from the compiler (I get like 5 of these errors):
./Tree.h:14:2: error: unknown type name 'Node'
Node *getParent(Node *child);
Here is my Tree.h
class:
#ifndef TREE_H_
#define TREE_H_
class Tree {
public:
Tree(int arr []);
Node *getParent(Node *child);
Node *getRightChild(Node *parent);
Node *getLeftChild(Node *parent);
private:
struct Node {
Node *parent;
Node *left;
Node *right;
int value;
};
Node *root;
int size;
};
#endif
Upvotes: 0
Views: 137
Reputation: 25388
Another way to get this code to compile is to forward declare Node
:
class Tree {
private: // add this
struct Node; // and this
public:
Tree(int arr []);
Node *getParent(Node *child);
Node *getRightChild(Node *parent);
Node *getLeftChild(Node *parent);
private:
struct Node {
Node *parent;
Node *left;
Node *right;
int value;
};
Node *root;
int size;
};
This works because you are only using pointers-to-Node (Node *
) in your outer class. If you wanted to use a bare Node
then you would have to use the other solution here.
Upvotes: 0
Reputation: 580
Change the arrangement of code to this and it should work fine:
#ifndef TREE_H_
#define TREE_H_
class Tree {
private:
struct Node {
Node *parent;
Node *left;
Node *right;
int value;
};
Node *root;
int size;
public:
Tree(int arr []);
Node *getParent(Node *child);
Node *getRightChild(Node *parent);
Node *getLeftChild(Node *parent);
};
The compiler reads the code in sequence. In your case, you have defined your Node
struct after defining your functions (which are using instances of your Node
struct) and hence, it is unable to recognize the Node
type.
Declaring your Node
struct before declaring your functions will make your code work.
Upvotes: 1