error: field has incomplete type

Asked

Viewed 658 times

1

I get this mistake

error: field has incomplete type

by doing something like:

class MinhaClasse
{
   MinhaClasse teste; // Erro aqui
};

I already tried to add this line at the beginning of the code class MinhaClasse;, but it didn’t work.

I understand the mistake, but I don’t know how I can fix it.

  • 1

    what you really want to do?

  • @rLinhares The above code is just an example of the error, the complete code is very large, so it did not include in the question

  • 2

    I didn’t even know that C had classes. It would not be C++ ?

  • What is the purpose of MinhaClasse?

  • @lazyFox This is just one example.

1 answer

4


The error already tells the problem. To set MinhaClasse need to define MinhaClasse, and goes into loop infinite, has no solution.

Actually there is a solution, is to turn the field into a pointer, then he knows exactly how to compose the field since he is a pointer, something that the compiler already knows. That is, a indirect solves the problem. It may not be what you want, but it is the only way.

class MinhaClasse { MinhaClasse *teste; };

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

or

class MinhaClasse { MinhaClasse unique_ptr<teste>; };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.