Incomplete type error

I am trying to make class A a friend of class B.

class B;

class A{
public:
void show(const B&); // ##1## but this one works fine  
B ob;// error incomplete type

};


class B{
public:
int b;
B():b(1){}
friend class A;  

};

so my question is, why is he an incomplete type? I thought that when I did class Bit, as a prototype of a function that tells compilation, there is a definition somewhere in the code.

also in the code above in ## 1 ## why is this possible?

+3
source share
1 answer

No, this declaration is forward and does not define the full type. You will need to have a full definition Bbefore Aif you want to save the element as an object, not a pointer.

One reason for this is that the size of the class Bmust be known A, since the size Adepends on B.

I suggest you #include "B.h"to A.h.

EDIT: :

struct A;

struct B
{
   A foo();
   void foo(A);
   void foo(A&);
   void foo(A*);

   A* _a;
   A& __a;
   A a;  // <--- only error here
};
+9

All Articles