39

I have a class that should have a private member of the same class, something like:

class A {
    private:
        A member;
}

But it tells me that member is an incomplete type. Why? It doesn't tell me incomplete type if I use a pointer, but I'd rather not use a pointer. Any help is appreciated

nbro
  • 12,226
  • 19
  • 85
  • 163
Sterling
  • 3,585
  • 13
  • 44
  • 72

8 Answers8

43

At the time you declare your member, you are still defining the A class, so the type A is still undefined.

However, when you write A*, the compiler already knows that A stands for a class name, and so the type "pointer to A" is defined. That's why you can embed a pointer to the type your are defining.

The same logic applies also for other types, so if you just write:

class Foo;

You declare the class Foo, but you never define it. You can write:

Foo* foo;

But not:

Foo foo;

On another hand, what memory structure would you expect for your type A if the compiler allowed a recursive definition ?

However, its sometimes logically valid to have a type that somehow refer to another instance of the same type. People usually use pointers for that or even better: smart pointers (like boost::shared_ptr) to avoid having to deal with manual deletion.

Something like:

class A
{
  private:
    boost::shared_ptr<A> member;
};
ereOn
  • 48,328
  • 33
  • 147
  • 228
  • I am making a maze solving program and I wanted a class Position to have a previous Position member. – Sterling Jun 14 '11 at 20:47
  • @Sterling: I have updated my answer to indicate a possible solution. – ereOn Jun 14 '11 at 20:51
  • 11
    @Sterling: It may make more sense for the _owning_ class to contain both "nowPosition" and "previousPosition" members. A position is a position; it doesn't need to know about other positions. – Lightness Races in Orbit Jun 14 '11 at 21:19
  • 1
    @CrazyPython The reason Python allows this is because it uses references, which is equivalent to the `shared_ptr` solution I mentionned. Note that doing that usually causes cyclic reference problems which can prevent the garbage collection from doing its job so it's not a silver bullet either. – ereOn May 09 '16 at 14:55
28

This is a working example of what you are trying to achieve:

class A {
public:
    A() : a(new A()) {}
    ~A() { delete a; a = nullptr; }
private:
    A* a;
};

A a;

Happy Stack Overflow!

Community
  • 1
  • 1
Nick
  • 5,655
  • 5
  • 25
  • 35
  • 4
    Please delete the inner `A* a`. on class destruction. We don't need examples teaching young programmers to leak memory more than they will do themselves. – Arelius Oct 09 '13 at 19:51
  • 1
    Actually, this allocates on the heap, so it will originate both a stack overflow and an out of memory error, whatever comes first. – jv110 Dec 21 '17 at 02:11
5

A is "incomplete" until the end of its definition (though this does not include the bodies of member functions).

One of the reasons for this is that, until the definition ends, there is no way to know how large A is (which depends on the sum of sizes of members, plus a few other things). Your code is a great example of that: your type A is defined by the size of type A.

Clearly, an object of type A may not contain a member object that is also of type A.

You'll have to store a pointer or a reference; wanting to store either is possibly suspect.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
4

A simple way to understand the reason behind class A being incomplete is to try to look at it from compiler's perspective.

Among other things, the compiler must be able to compute the size of A object. Knowing the size is a very basic requirement that shows up in many contexts, such as allocating space in automatic memory, calling operator new, and evaluating sizeof(A). However, computing the size of A requires knowing the size of A, because a is a member of A. This leads to infinite recursion.

Compiler's way of dealing with this problem is to consider A incomplete until its definition is fully known. You are allowed to declare pointers and references to incomplete class, but you are not allowed to declare values.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
2

You cannot include A within A. If you were able to do that, and you declared, for example, A a;, you would need to refer to a.member.member.member... infinitely. You don't have that much RAM available.

mah
  • 37,085
  • 9
  • 70
  • 91
1

How can an instance of class A also contain another instance of class A?

It can hold a pointer to A if you want.

Andrei
  • 4,592
  • 22
  • 28
1

This type of error occurs when you try to use a class that has not yet been fully DEFINED.

Try to use A* member instead.

trema
  • 11
  • 2
0

The problem happens when the compiler comes across an object of A in code. The compiler will rub its hand and set out make an object of A. While doing that it will see that A has a member which is again of type A. So for completing the instantiation of A it now has to instantiate another A ,and in doing so it has to instantiate another A and so forth. You can see it will end up in a recursion with no bound. Hence this is not allowed. Compiler makes sure it knows all types and memory requirement of all members before it starts instantiating an object of a class.

Sushovan
  • 21
  • 1
  • 6