Class with same type member object

Hi,

I have the following class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef NODE_H
#define	NODE_H

#include <iostream>

class Node {
private:
    Node leftChild;
    Node rightChild;
public:
    Node();
};

#endif	/* NODE_H */ 


As seen, I have to objects of the same type of the class (Node). When I run this program, I get a ''Field has incomplete type error''.

Now I guess it makes sense, because this process is ongoing. But is this what is causing the error?

I fixed it by making leftChild and rightChild a pointer and created the objects by calling a separate function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef NODE_H
#define	NODE_H

#include <iostream>

class Node {
private:
    Node* leftChild;
    Node* rightChild;
public:
    Node();
    void createLeftChild();
    void createRightChild();
};

#endif	/* NODE_H */


But I just wondered: is it because it is an ongoing process that causes the field has incomplete type (leftChild, rightChild)?

Thank you!
The size and layout of an object of an incomplete type is unknown.
We cant define an object of an incomplete type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
struct A ; // A is an incomplete type (known in name, but not known in size)
// A is a class that has been declared but not defined

extern A object ; // fine; we can declare an object of an incomplete type

// A object ; // ****error: A is an incomplete type, can't define an object
// int sz = sizeof(A) ;  // ****error: A is an incomplete type, size is not known

A* pointer = &object ; // fine, size and layout of pointer is known
A& reference = object ; // fine, we can create an alias for an object of an incomplete type.

extern A array[20] ; // fine; declaration of an object of incomplete type

// int sz_array = sizeof(array) ; // ****error: A[20] is of an incomplete type, size is not known

struct B
{
    B( A& a ) : member3(a) {} // fine
    
    // A member ; // ****error: A is an incomplete type, size and layout are not known
     
    A* member2 = nullptr ; // fine
    A& member3 ; // fine
};

struct A // start definition of A
{
     // A is still an incomplete type at this point

     void foo( int v = sizeof(A) ) ; // fine, sizeof(A) need not be known at this point

     // char cstr[ sizeof(A) ] ; // ***error: sizeof A is not known

     // A member ; // ****error: A is an incomplete type, size and layout are not known
     
     A* member2 = this ; // fine
     A& member3 = *this ; // fine

}; // end definition of A

// at this point, A has become a complete type

int sz_array = sizeof(array) ; // fine

A array[20] ; // fine

A object ; // fine;

int sz = sizeof(A) ;  // fine 
If it was possible just imagine how big a Node object would be. Node contains two nodes that contains two nodes each that also contains two nodes each and so on. The Node object would have to be infinite large. Todays computers have finite amount of memory so that's not going to work. That's why you are forced to use pointers of some kind instead.
Thanks for the replies!

But when I have declared a pointer like the following as a member variable, and I don't use a function to allocate it.
Wouldn't it give me the same problem, but now that it is declaring pointers ongoingly?

 
Node* leftChild;


Last edited on
The size of the pointer is known. It's the same no matter what size Node has.
Ah wait of course I understand now!

Thank you very much!
Last edited on
Topic archived. No new replies allowed.