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
How can an instance of
class A
also contain another instance ofclass A
?It can hold a pointer to A if you want.
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 typeA
is defined by the size of typeA
.Clearly, an object of type
A
may not contain a member object that is also of typeA
.You'll have to store a pointer or a reference; wanting to store either is possibly suspect.
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 toa.member.member.member...
infinitely. You don't have that much RAM available.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.
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 operatornew
, and evaluatingsizeof(A)
. However, computing the size ofA
requires knowing the size ofA
, becausea
is a member ofA
. 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.This is a working example of what you are trying to achieve:
Happy Stack Overflow!