c++ - error C2079: 'Toto::a' uses undefined class 'foo' -


this question has answer here:

i have error c2079 on compiling following code , dont understand why. class foo declared later (forward declaration).

class foo;  //template <typename datatype> class toto {     foo a; //c2079 };  class foo { public: int x; }; 

and strange in issue, if uncomment "template line" (before class toto declaration), error disapear. use workaround, don't understand happen here.

following first feedback got, tried following code :

class foo;  //template <typename datatype> class toto {     foo *a; // solve c2079          void otherfunc()         {             a->myfunc(); // c2027         } };  class foo { public: int x;     void myfunc()     {     }; }; 

so replacing "foo a" pointer "foo * a" solve compilation error. adding function it's implementation , calling "a->myfunc()" prroducing "error c2027: use of undefined type 'foo'". similar issue ? , again "template" solve it. , yes, use msvc compiler.

i dont understand why

because create class have value member, value member must defined time it's used. (it's necessary e.g. calculate size of class). if pointer or reference, fine.

and strange in issue, if uncomment "template line" (before class toto declaration), error disapear.

as @angew points out, might happen non-compliant compiler. example, g++ outputs proper error diagnostic:

main.cpp:7:9: error: field 'a' has incomplete type 'foo'      foo a;          ^ 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -