我有 A 类和 B 类,它们的头文件都带有包含保护。一读:
#ifndef A_H
#define A_H
#include "B.h"
class A
{
B b;
};
#endif
还有一个:
#ifndef B_H
#define B_H
#include "A.h"
class B
{
A a;
};
#endif
现在我使用以下 main.cpp 对其进行测试:
#include "A.h"
int main()
{
A a;
}
编译错误如下:
# make main
g++ main.cpp -o main
B.h:8: error: ‘A’ does not name a type
除了使用指针/引用和前向声明之外,有没有解决这种情况的方法?