“Incomplete type” in class which has a member of the same type of the class itself

谁说我不能喝 提交于 2019-12-17 02:12:23

问题


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


回答1:


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;
};



回答2:


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!




回答3:


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.




回答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.




回答5:


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.




回答6:


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

It can hold a pointer to A if you want.




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/6349822/incomplete-type-in-class-which-has-a-member-of-the-same-type-of-the-class-itse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!