a.h
class A;
class B{
public:
int i;
A a;
};
class A{
public:
int k;
B b;
};
nest.cpp
#include "A.h"
int main(){
A a;
B b;
return 1;
}
A error "field `a' has incomplete type" generates.
根据这个定义,A和B的内存布局都不能确定,所以编译不通过。
更改的方法:
a.h
class A;
class B{
public:
int i;
A * a;
};
class A{
public:
int k;
B * b;
};
nest.cpp
#include "B.h"
#include "A.h"
int main(){
A a;
B b;
return 1;
}
使用指针后,内存布局能确定,编译通过。
a good blog: http://dev.csdn.net/htmls/60/60072.html