循环C ++头包括 [英] Circular C++ Header Includes

查看:168
本文介绍了循环C ++头包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a project I have 2 classes:

// mainw.h

// 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];
};

但是我无法编译这个代码,在 ; 行。是否禁止这种交叉包含?

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;
...
}

头文件(以便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 ++头包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆