I’ve been reading some on forward declarations, including in this forum. They all say that it saves us from including the header file, However the following code generates an error:
#ifndef CLASSA_H_
#define CLASSA_H_
class B;
class A {
public:
A();
~A();
int getCount();
private:
static int _count;
int _num;
B _b1; //ERROR
};
compiler says:
A.h:23: error: field ‘_b1’ has incomplete type
I noticed that if i make _b1 of type B* the problem is solved.
So is forward declaration good only for pointer types?
If i want A to hold B object i have to #inlcude "B.h" ?
thanks!
The compiler has to know the exact definition of class
Bto determine at least what size to give to classA. If you use a pointer, it knows its size.Note that circular dependencies are not possible. If you want
then A and B must have infinite size…