问题描述
在C ++中,我有一个双重包含问题:
文件stuffcollection.h
#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H
#includeStage.h
class Stuffcollection {
public:
bool myfunc(Stage * stage);
};
#endif // STUFFCOLLECTION_H
档案stage.h:
#pragma once
#ifndef STAGE_H
#define STAGE_H
#includeStuffcollection .h
class Stage {
//在stage.cpp中使用的stuffcollection
};
#endif // STAGE_H
编译器错误:
\Stuffcollection.h |(行被bool myfunc声明)|错误:'Stage'尚未声明|
|| === Build finished:1 errors,0 warnings === |
有人可以解释为什么会发生这种情况以及如何解决呢?我已经使用include guard和pragma once preprocessor指令,它只是不工作。
(如果我删除 #includeStuffcollection.h
from stage.h并注释掉在stage.cpp中使用它的各行,我的其余代码工作正常,它只是当Stuffcollection在阶段,它突然停止工作)。
PS:stage只是一个例子,我在几乎所有其他文件中使用stuffcollection,每次我遇到这个问题。 >
EDIT :我遵循了建议,现在问题是无效使用不完整类型
,即,虽然给出的答案解决了循环依赖的问题,但它们不能解决我正在处理的问题。我的问题在中继续。
EDIT :现在解决了。
您有圆形依存关系。请改用 Stuffcollection.h
#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H
//#includeStage.h//< ---------- --------不要这样做
class Stage; //< ------------------ Do This
class Stuffcollection {
public:
bool myfunc(Stage *阶段);
};
#endif // STUFFCOLLECTION_H
原因:
您可以使用上述代码段中的向前声明,因为 Stuffcollection.h
只使用指向 Stage
。
说明:
使用类 Stage
,编译器不知道它的组成或其中的成员,编译器只知道 Stage
是类型
。因此,
Stage
是编译器的不完整类型。使用不完全类型,不能创建它的对象或做任何需要编译器知道 Stage
的布局或者比事实 Stage
只是一个类型。由于指向所有对象的指针只需要相同的内存分配,因此只需引用 Stage
作为指针即可使用forward声明。
您可以使用Forward声明来解决循环依赖性问题。
进一步阅读:
In C++, I have a problem with a double include:
File stuffcollection.h
#pragma once
#ifndef STUFFCOLLECTION_H
#define STUFFCOLLECTION_H
#include "Stage.h"
class Stuffcollection {
public:
bool myfunc( Stage * stage );
};
#endif // STUFFCOLLECTION_H
File stage.h:
#pragma once
#ifndef STAGE_H
#define STAGE_H
#include "Stuffcollection.h"
class Stage {
// stuffcollection used in stage.cpp
};
#endif // STAGE_H
Compiler Error:
\Stuffcollection.h|(line were bool myfunc is declared)|error: 'Stage' has not been declared|||=== Build finished: 1 errors, 0 warnings ===|
Can someone please explain why this happens and how it can be solved? I already use include guards and the pragma once preprocessor directive and it just doesn't work.
(If I remove #include "Stuffcollection.h"
from stage.h and comment out the respective lines that are using it in stage.cpp, the rest of my code works fine. It's really just when including Stuffcollection into stage that it suddenly stops working.)
PS: stage is just one example, I use stuffcollection in almost every other file too, and everytime I get this problem.
EDIT: I followed what has been suggested, and now the problem is invalid use of incomplete type
, i.e. while the answers given solve the problem of the circular dependency they do not solve the problem I am dealing with. My problem is continued in Circular Dependencies / Incomplete Types.
EDIT: Both solved now.
You have a Circular Dependency. Instead use Forward Declaration in 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
Rationale:
You can use the forward declaration in above snippet because, Stuffcollection.h
only uses pointer to Stage
.
Explanation:
Using a forward declaration of class Stage
, the compiler does not know the composition of it nor the members inside it, the compiler only knows that Stage
is a type
. Thus,Stage
is an Incomplete type for the compiler. With Incomplete types , One cannot create objects of it or do anything which needs the compiler to know the layout of Stage
or more than the fact that Stage
is just an type. Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just referring to Stage
as a pointer.
You can use Forward Declarations to get over your circular dependency problems.
Further Read:
When to use forward Declarations?
这篇关于双包括解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!