in Line 3 “Node next;” the Compiler gives me an incomplete type error. I think it treat it as a member function not an Attribute. What’s wrong in that Definition?
class Node {
private:
Node next;
Node previous;
int value;
int min;
public:
Node(int value) {
this.value = value;
this.min = value;
}
void insert(Node node) {
if (this.next != null) {
this.next.insert(node);
}
else {
this.next = node;
node.previous = this;
}
}
}
First of all,
thisis a pointer, not a reference, so you need to replace allthis.withthis->.Secondly, you cannot store an instance of a class inside that class because the size of the class would be incalculable. You can only store pointers or references to the class inside the class itself, because the compiler can calculate the size of a pointer or reference to any class without having to have any details of the class (because these pointers/references are all the same size regardless of the underlying object). Change
to
And change
to
You also need to initialise the member pointers to
NULL(notnull) in the constructor of your object (because C++ doesn’t initialise variables that are intrinsic (built-in) types (like pointers) for you):Additionally, I (like everyone else) have deduced (from your use of objects like they are references and your use
nullinstead ofNULLand of the words ‘method’ and ‘attribute’) that you are a Java or C# programmer learning C++. Please stop now and read a good book on C++, because C++ is nothing like Java or C#, and if you try to use your C#/Java experience when programming C++ then you will only end up ripping your own head off out of frustration.