C++循环包括内部结构
C++ circular includes with internal structs
我有一个关于循环包含的问题,这让我很疯狂:
主.cpp
#include "A.hpp"
#include "B.hpp"
int main()
{
A a();
B b();
return 0;
}
A.hpp
#ifndef _CLASS_A
#define _CLASS_A
#include "B.hpp"
class A
{
public:
B* b;
struct A_t
{
int id;
};
};
#endif
B.马力
#ifndef _CLASS_B
#define _CLASS_B
#include "A.hpp"
class B
{
class A; //Ok, with that I can use the class A
public:
int a;
A* b; // That work!
A::A_t *aStruct; // Opss! that throw a compilation error.
};
#endif
问题是:¿如何在 B 类中使用 A_t 结构?
我试图添加一个转发声明,例如:
struct A::A_t;
但这显然确实有效。
将
A.h
中的包含替换为前向声明。
#ifndef _CLASS_A
#define _CLASS_A
class B;
class A
{
public:
B* b;
struct A_t
{
int id;
};
};
#endif
另外,请注意
A a();
B b();
不会创建类的两个实例,但它们是函数声明。你想要
A a;
B b;
相关文章:
- 为什么未命名的结构内联变量在每个翻译单元中没有相同的地址?
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 如何将字符串与结构(也包括字符串)进行比较?
- 结构内向量的初始化
- 按字母顺序对结构内数组变量中的名称进行排序
- 在向量 C++14(无限制联合)的结构内的联合中创建和存储字符串
- 尝试初始化结构内的联合时出错
- 简单的问题 - 如何从单独的"结构"内的'class'访问变量?
- 无法调用结构内的成员函数指针
- 实现具有浮点键的类似哈希表的数据结构,其中公差内的值被合并在一起
- 具有指向函数的指针的类内的结构是否需要类外函数的前向声明
- C++标准是否定义了结构中成员函数的函数内定义是否必须具有静态链接?
- 如何计算结构阵列内的平均值
- 结构C++内的函数
- erase() 不适用于结构/对象内的 STL 向量
- 结构/对象内的存储顺序
- Visual Studio中的嵌套文件夹结构-包括文件
- 两个相关结构的内联方法
- C++模板自身名称作为模板模板参数在同一模板结构函数内传递
- 在 GNU 的C++代码中使用 C 标头。包括内联装配的错误:'asm'中不可能的约束