error: field has incomplete type

1

I get this error

  

error: field has incomplete type

when 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 did not work.

I understand the error, but I do not know how I can resolve it.

    
asked by anonymous 01.02.2018 / 15:39

1 answer

4

The error already says the problem. To set MinhaClasse need to set MinhaClasse , and enter loop infinite, there is no solution.

In fact there is a solution, it is to transform the field into a pointer, then it knows exactly how the field is composed since it is a pointer, something that the compiler already knows. That is, a indirection solves the problem. It may not be what you want, but it's the only way.

class MinhaClasse { MinhaClasse *teste; };

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

or

class MinhaClasse { MinhaClasse unique_ptr<teste>; };
    
01.02.2018 / 16:41