2

I have error C2079 on compiling following code and I dont understand why. class foo is declared later (forward declaration).

class foo;

//template <typename dataType>
class Toto
{
    foo a; //C2079
};

class foo
{
public:
int x;
};

And what is really strange in that issue, is if I uncomment the "template line" (before class Toto declaration), the error disapear. I could use it as a workaround, but I don't understand what happen here.

Following first feedback I got, I tried the 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" by a pointer "foo * a" solve the compilation error. But adding a function with it's implementation and calling "a->myFunc()" is now prroducing "error C2027: use of undefined type 'foo'". Is it similar issue ? And again "Template" solve it. And yes, I use MSVC compiler.

  • Thanks R Sahu to point me on this link, was not obvious to find. Yes, I think my cases are covered there and I agree it's duplicated. Maybe a reference to C2079 and C2027 compiler errors may make it simpler to find. – Thierry Campiche Oct 27 '15 at 17:12

1 Answers1

4

I dont understand why

Because to create a class where you have a value member, that value member must be defined by the time it's used. (It's necessary e.g. to calculate the size of the class). If that was a pointer or a reference, it would be fine.

And what is really strange in that issue, is if I uncomment the "template line" (before class Toto declaration), the error disapear.

As @Angew points out, this might happen with a non-compliant compiler. For example, g++ outputs proper error diagnostic:

main.cpp:7:9: error: field 'a' has incomplete type 'foo'
     foo a;
         ^
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135