Circular C++ Header Includes(循环 C++ 头文件包括)
问题描述
在一个项目中,我有 2 个类:
In a project I have 2 classes:
//mainw.h
#include "IFr.h"
...
class mainw
{
public:
static IFr ifr;
static CSize=100;
...
};
//IFr.h
#include "mainw.h"
...
class IFr
{
public float[mainw::CSize];
};
但我无法编译此代码,在 static IFr ifr; 行出现错误.这种交叉包含是否被禁止?
But I cannot compile this code, getting an error at the static IFr ifr; line. Is this kind of cross-inclusion prohibited?
推荐答案
这种交叉包含是否被禁止?
Is this kind of cross-inclusions are prohibited?
是的.
一种变通方法是说 mainw 的 ifr 成员是一个引用或一个指针,这样前向声明就可以了,而不是包含完整的声明,例如:
A work-around would be to say that the ifr member of mainw is a reference or a pointer, so that a forward-declaration will do instead of including the full declaration, like:
//#include "IFr.h" //not this
class IFr; //this instead
...
class mainw
{
public:
static IFr* ifr; //pointer; don't forget to initialize this in mainw.cpp!
static CSize=100;
...
}
或者,在单独的头文件中定义 CSize 值(以便 Ifr.h 可以包含这个其他头文件,而不是包含 mainw.h).
Alternatively, define the CSize value in a separate header file (so that Ifr.h can include this other header file instead of including mainw.h).
这篇关于循环 C++ 头文件包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:循环 C++ 头文件包括
基础教程推荐
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
