0

Here is my code:

class Link;
class LinkScreen;
class LinkScreen {
    Link* linkScreen1;
    LinkScreen linkScreen2; 
};

class Link {}; 

The make error:

test.cpp:6:16: error: field ‘linkScreen2’ has incomplete type

Why is this not allowed?

Sam
  • 964
  • 3
  • 11
  • 24
  • 1
    Because contrary to philosophers, it's *not* turtles all the way down. – WhozCraig Mar 13 '14 at 03:27
  • The compiler must know the size of the class object if define an instance, it can't be done in its own class definition. So use pointer or static member is ok. – A Lan Mar 13 '14 at 03:38
  • possible duplicate of [why can't we declare object of a class inside the same class?](http://stackoverflow.com/questions/4941629/why-cant-we-declare-object-of-a-class-inside-the-same-class) – Alastair Irvine Mar 13 '14 at 03:47

4 Answers4

4

A class can't contain an instance of itself because that would make the instances take up an infinite amount of space.

Think about it: you create a LinkScreen object… which contains another LinkScreen object… which contains another LinkScreen object, which contains yet another, and so on.

Or, to look at it another way, what's the size of a LinkScreen object? Well, it's the size of the variables it contains: a Link* (typically 4 or 8 bytes) plus the size of a LinkScreen object. But how big is that? Well, it's the size of a Link* plus the size of a LinkScreen. You can see the infinite recursion here.

You can only create an instance of a type that's "complete", which for a class means that the compiler has seen the closing brace of the class definition. That prevents you from putting an instance within the class itself. You can create a pointer to an incomplete type, though; it's OK for a LinkScreen object to contain a LinkScreen* variable.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • +1 That was one very useful comment you made on the now extinct *dynamic memory allocation of 1 char actually stores more than 1 character* thread. – karlphillip Mar 13 '14 at 04:17
1

Duplicate: why can't we declare object of a class inside the same class?

Because it requires knowing the size of each of the constituent members of the class linkScreen. In your case size of linkScreen2 is not known, since the class is not completely defined at that very moment.

Community
  • 1
  • 1
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
0

Your problem is that your LinkScreen object contains a LinkScreen object, which then contain a LinkScreen object which then contain a LinkScreen object and so on. I guess it should be a pointer instead.

MTilsted
  • 5,425
  • 9
  • 44
  • 76
0

Because it is simply illogical:

I suppose you mean LinkScreen * linkScreen2;or LinkScreen& linkScreen2;

If you really want LinkScreen linkScreen2;, then just imagine: A LinkScreen object is going to contain (not referring to) a LinkScreen object, which in turns contain a LinkScreen object..... It is never going to end.


A bit more on having another class as member.

Unlike having pointer/ref to another class, if you want to contain another class as member, having only forward declaration is not enough. Compiler needs to know the actual "layout" of your members, so it can "decide" the "layout of memory" of that class. Therefore you need a complete declaration of the "contained class" available before your class. Which means, it is never going to be possible to have your class "contains" a member of same type.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131