循环C++标题包含

pau*_*ons 14 c++ static circular-dependency include

在一个项目中,我有两个类:

// mainw.h

#include "IFr.h"
...
class mainw
{
public:
static IFr ifr;
static CSize=100;
...
};
Run Code Online (Sandbox Code Playgroud)

// IFr.h

#include "mainw.h"
...
class IFr
{
public float[mainw::CSize];
};
Run Code Online (Sandbox Code Playgroud)

但我无法编译此代码,在该static IFr ifr;行收到错误.是否禁止这种交叉包含?

Chr*_*isW 16

是否禁止这种交叉包含?

是.

解决方法是说mainw的ifr成员是引用或指针,因此前向声明将执行而不是包括完整声明,如:

//#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;
...
}
Run Code Online (Sandbox Code Playgroud)

或者,在单独的头文件中定义CSize值(以便Ifr.h可以包含此其他头文件而不是包括mainw.h).