Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
双包解决方案? 在C++中,我有一个双重问题:_C++_Include_Include Guards - Fatal编程技术网

双包解决方案? 在C++中,我有一个双重问题:

双包解决方案? 在C++中,我有一个双重问题:,c++,include,include-guards,C++,Include,Include Guards,文件stuffcollection.h #pragma once #ifndef STUFFCOLLECTION_H #define STUFFCOLLECTION_H #include "Stage.h" class Stuffcollection { public: bool myfunc( Stage * stage ); }; #endif // STUFFCOLLECTION_H 文件stage.h: #pragma once #ifndef STAGE

文件stuffcollection.h

#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H

#include "Stage.h"

class Stuffcollection {
    public:
        bool myfunc( Stage * stage );
};

#endif // STUFFCOLLECTION_H
文件stage.h:

#pragma once
#ifndef STAGE_H
#define STAGE_H

#include "Stuffcollection.h"

class Stage {
    // stuffcollection used in stage.cpp
};

#endif // STAGE_H
编译器错误:

\Stuffcollection.h |(声明了行WARE bool myfunc)|错误:“阶段”尚未声明|
||===构建完成:1个错误,0个警告===|

有人能解释一下为什么会发生这种情况以及如何解决吗?我已经使用了include-guards和pragma-once预处理器指令,但它根本不起作用

(如果我从stage.h中删除
#include“Stuffcollection.h”
,并注释掉stage.cpp中使用它的相应行,我的其余代码工作正常。实际上,当将Stuffcollection包含到stage中时,它突然停止工作。)

PS:stage只是一个例子,我在几乎所有其他文件中也使用stuffcollection,每次我遇到这个问题

编辑:我遵循了建议,现在的问题是
不完整类型的无效使用
,也就是说,虽然给出的答案解决了循环依赖的问题,但它们并没有解决我正在处理的问题。我的问题还在继续


编辑:两者现在都已解决。

这是一个循环依赖项。不要让他们。您可以通过将stuffcollection.h中的
#include“Stage.h”
替换为
Stage
(即
类Stage;
)的前向声明来解决此问题。

您可以使用前向声明,即
类Stage
类填充集合
。它还有减少依赖性的好处。

您有一个循环依赖性。在
Stuffcollection.h

#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H

//#include "Stage.h"      //<------------------Don't Do This
class Stage;              //<------------------Do This

class Stuffcollection {
    public:
        bool myfunc( Stage * stage );
};

#endif // STUFFCOLLECTION_H
#pragma一次
#ifndef填充物收集
#定义填充集合

//#包括“Stage.h”//对于前两句中的合理建议,这值得+1。我可能没有说得足够清楚,但我需要能够在Stage中创建StufkCollection对象(或至少使用传入的StufkCollection对象的方法),反之亦然。现在我的问题是“不完整类型的无效使用”。@Ben:你的Q实际上没有提到任何内容,你读过链接了吗?它将为您提供一个完整的列表,列出您可以/t处理不完整类型的操作。如果您有不完整类型的特定错误案例,请将其作为问题发布。对不起,如果我不打算使用它,为什么我要包含它。不管怎样,谢谢你的回答,我想这是解决循环依赖问题的方法,而不是解决我的问题。@Ben:我想是的,也许你只是混淆了两个问题,是的,这是解决循环依赖问题的方法。对于你的另一个问题,请发布一个新的问题和细节,这将使两个不同的事情分开,并保证你有一个适当的解决方案。