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++ 头文件包括
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
