Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomplete Type Is Not Allowed

Tags:

c++

Attempting to create as simple parent/child node class but running into Incomplete Type Is Not Allowed error. Why?

class Node {
public:
    Node ParentNode;  //Error on this line
    string NodeName;


    Node(Node *node) : ParentNode(*node) {

    }
};
like image 828
user3745593 Avatar asked Feb 12 '26 13:02

user3745593


2 Answers

You are trying:

Node ParentNode;  //Error on this line

but Node is not a complete type at this point (you are in fact defining it at this point), a structure cannot contain an instance of itself, it can contain a pointer or reference to an instance of itself but not an actual instance. After all, if such recursive containment were allowed, where would it end?

like image 200
SoronelHaetir Avatar answered Feb 14 '26 21:02

SoronelHaetir


The error hints at the fact that the definition of a class is incomplete while the class is being defined.

An object can't contain an instance of its own type – it would be infinitely large.

You want

Node* ParentNode;

and

Node(Node *node) : ParentNode(node)
like image 40
molbdnilo Avatar answered Feb 14 '26 21:02

molbdnilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!