定义了两个头文件 //a.h #include"b.h" class a { … b *b1; }; //b.h #include "a.h" class b { … a *a1; };
这样子是编译不了
解决方案:
不需要这么包含,除非必要 如果你只是想在另外一个类中定义一个类的成员变量,只要在头文件中 加入 class 类名; 然后要在.cpp文件中包含这个头文件就可以了 第二种解决方案: 加入宏定义 #ifndef __A_H__ #define __A_H__ class b; class a { ... } #endif b.h #ifndef __B_H__ #define __B_H__ class a; class b { ... } #endif因为.h中是一种强引用,彼此引用类是不可以编译通过的,最好的可以采用第一套方案