0

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;
                }
            }
}

6 Answers6

4

First of all, this is a pointer, not a reference, so you need to replace all this. with this->.

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

Node next;
Node previous;

to

Node* next;
Node* previous;
// or Node* next, *prev;

And change

void insert(Node node)

to

void insert(Node* node)

You also need to initialise the member pointers to NULL (not null) in the constructor of your object (because C++ doesn't initialise variables that are intrinsic (built-in) types (like pointers) for you):

Node(int value) {
    this->previous = NULL;
    this->next = NULL;     // or: previous = next = NULL;

    this->value = value;
    this->min = value;     // or: this->value = min = value;
}

// or with initialiser lists (but don't get confused if you don't understand)
// Node(int value) : previous(), next(), value(value), min(value) { }

Additionally, I (like everyone else) have deduced (from your use of objects like they are references and your use null instead of NULL and 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.

Community
  • 1
  • 1
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • @DmitryKachko yes, in this case we definitely need to – Seth Carnegie Feb 19 '12 at 15:52
  • Saying that "the size would be incalculable" is just a cowardish excuse. The real problem is that a class containing a member of its own type just plain doesn't make any sense. – Kerrek SB Feb 19 '12 at 16:04
2

Work with pointers, not objects.

Node * next;
Node * previous; 

then

 Node(int value) : next(NULL), previous(NULL) {/**/}

then

void insert(Node * node) {/**/}
Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21
1

It looks like you're trying to write Java/C# in C++!

In C++, objects are stored by value, not by reference. So in something like this:

class Node {
    Node next;
};

you're asking for a Node to store another Node inside it, which would require an infinite amount of storage.

Perhaps you want to store pointers to the next and previous nodes.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

C++ does not have a concept of "class attributes". Any variable declared at class scope is a (possibly static) member. Therefore, a class that contains a member of its own type is an absurdity. For example,

struct S {
    S s;
    int i;
};

being legal would mean that sizeof(S) == sizeof(S) + sizeof(int), ignoring padding. As this clearly cannot be the case, it is forbidden.

If you want a Node to have pointers to other Nodes (which you probably do), you should give it Node* members. You should also be passing a Node* to insert, as opposed to a Node*.

By the way, C++ does not generally have null defined like C# does. You can compare pointers to NULL, 0, or nullptr.

Cactus Golov
  • 3,474
  • 1
  • 21
  • 41
0

The class Node is being defined and while doing so, you cannot define any member of type Node, because the size of Node is not yet determined. Just think, how would compiler computer the size of Node? To compute sizeof(Node), the compiler needs to know the size of each of its members, but two of its members are of type Node itself whose size is being computed.

The solution is to define them as pointer :

Node *next;
Node *previous;

Now the size of Node is computable, as size of each of its members is already known. Note that size of pointer of any type is equal to sizeof(void*)1, which means sizeof(Node*) is equal to sizeof(void*) which is known.

1. Except sizeof(pointer-to-member) could be different from sizeof(void*). Technically, for many compilers, pointer-to-member is not pointer to begin with; they are different animals.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

You can't nest objects like that. Think about it -- what is the size of Node? Well, since each Node contains exactly 2 other Nodes, it means it would have infinite size.

You need a way to make your children null, so the solution is to use Node* instead of Node.

tenfour
  • 36,141
  • 15
  • 83
  • 142