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