简体   繁体   中英

field has incomplete type C++

I am trying to make class that has a member of type of it's enclosing class but I get an error saying field has incomplete type. here is an example

class List {
public:
        List (int element, List rest) {
                 _first = element;
                 _rest = rest;
         }
.
.
.
}

Is there a way to fix this problem?

You haven't given us the complete definition of List , but I'm guessing from your description that you have something like:

class List
{
    ...
    List _rest;
};

Obviously, this is impossible. An object cannot contain a member of its own type, as this would lead to an infinite recursion!

Perhaps you want a member that is a pointer or a reference?

Use this:

'rest' should be a pointer to a List .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM