0

I know the following code is not correct, but I really want to know WHY it isn't possible.

struct nod {
    int val;
    nod a;
};

Of course, I should've made it nod *a, but why do I get field a has incomplete type when I compile? I can however make a a pointer in the structure which simulate linking to another portion of memory after I free some space using new operator.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
Samuel Cota
  • 57
  • 1
  • 2
  • 6
  • 4
    I think a better question would be why **should** it be possible? – John Dibling Nov 17 '14 at 20:22
  • node not nod - Also having a struct with a member of the struct's type? @JohnDibling said "Is it possible"? – ha9u63a7 Nov 17 '14 at 20:23
  • 3
    See e.g. [this answer](http://stackoverflow.com/questions/26528633/create-an-instance-of-a-class-in-the-class-itself/26528810#26528810). Related: [this recent question](http://stackoverflow.com/questions/26981242/why-do-i-get-this-error-when-initialising-a-stack). – molbdnilo Nov 17 '14 at 20:25
  • 2
    `sizeof(nod)` would be ~= to `sizeof(int) + sizeof(nod)`. It's recursive, so `sizeof(nod)` = `sizeof(int) + sizeof(nod)` = `sizeof(int) + sizeof(int) + sizeof(nod)` = `sizeof(int) + sizeof(int) + sizeof(int) + sizeof(nod)` = ... forever and ever. – Cornstalks Nov 17 '14 at 20:26
  • @molbdnilo: you just linked the question to itself... – Cornstalks Nov 17 '14 at 20:27
  • @hagubear: The question was not if it's possible. The question was why it's not possible. – John Dibling Nov 17 '14 at 20:28
  • 1
    @Cornstalks: See what he did there? – John Dibling Nov 17 '14 at 20:28

4 Answers4

2

The compiler's error message is quite clear:

 field 'a' has incomplete type 

By the time the compiler reaches that line of code, nod has not yet been fully defined. In order to have a member of type nod in a class, the compiler needs to know at minumum just how big a nod is. Since nod isn't fully defined yet, the compiler has no way of knowing how big nod will be.

The reason why using nod*instead of anodworks is because the compiler doesn't need to know what is in anodin order to know how big a pointer-to-nod` is.

Even if nod was fully-defined, you still couldn;t do this. One nod would have another nod, which would have another nod, and so on forever. It's like standing in front of a mirror with a mirror.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

Think about it: if nod contains another nod, which in turn contains another nod which... (ad libitum), nod effectively would be of infinite size!

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

Because the compiler knows how much memory needs to be allocated for nod* (usually 32-bit or 64-bit), even if nod itself is not fully defined yet. In other words, the compiler only needs to leave a 32-bit or 64-bit slots at that pointer field.

On the other hand, if the field is a nod, then the compiler must know how much memory nod object needs to hold, which is not possible when nod itself is not fully defined.

Ying Xiong
  • 4,578
  • 8
  • 33
  • 69
0

Your question is not clear enough regarding what you are willing to do.

An Incomplete type error means you are trying to declare a variable whose type has only been forward declared.

If you do a forward declaration only, such as:

class nod;

The compiler knows that nod is a class defined elsewhere. You can then declare a pointer variable of type nod*. You cannot dereference it, nor declare a variable of type nod. For this, the compiler needs the full class declaration:

class nod
{
    int someVariable;
    char someOtherVariable;
}
galinette
  • 8,896
  • 2
  • 36
  • 87