双包含解决方案?
在 C++ 中,我遇到双重包含问题:
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不稳定)|错误:“Stage”尚未声明| ||=== 构建完成:1 个错误,0 个警告 ===|
有人可以解释一下为什么会发生这种情况以及如何解决吗?我已经使用了 include Guards 和 pragma Once 预处理器指令,但它不起作用。
(如果我从 stage.h 中删除 #include "Stuffcollection.h"
并注释掉 stage.cpp 中使用它的相应行,则我的代码的其余部分可以正常工作。这实际上只是在包含时Stuffcollection 进入 stage,它突然停止工作。)
PS:stage 只是一个例子,我也在几乎所有其他文件中使用 stuffcollection,每次我遇到这个问题。
编辑 : 我遵循的是建议,现在的问题是无效使用不完整类型
,即虽然给出的答案解决了循环依赖问题,但它们没有解决我正在处理的问题。我的问题在循环依赖/不完整类型中继续。
编辑:现在都解决了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有一个循环依赖。而是在
Stuffcollection.h
中使用前向声明基本原理:
您可以在上面的代码片段中使用前向声明,因为
Stuffcollection.h
仅使用指向Stage
的指针。说明:
使用类
Stage
的前向声明,编译器不知道它的组成,也不知道它内部的成员,编译器只知道Stage
是一个type< /代码>。因此,
Stage
是编译器的不完整类型。对于不完整类型,我们无法创建它的对象或执行任何需要编译器了解Stage
布局的操作,或者除了Stage
只是一种类型这一事实之外。由于指向所有对象的指针只需要相同的内存分配,因此当仅将Stage
引用为指针时,您可以使用前向声明。您可以使用前向声明来克服循环依赖问题。
进一步阅读:
何时使用前向声明?
You have a Circular Dependency. Instead use Forward Declaration in
Stuffcollection.h
Rationale:
You can use the forward declaration in above snippet because,
Stuffcollection.h
only uses pointer toStage
.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 thatStage
is atype
. 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 ofStage
or more than the fact thatStage
is just an type. Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just referring toStage
as a pointer.You can use Forward Declarations to get over your circular dependency problems.
Further Read:
When to use forward Declarations?
这是一个循环依赖。不要制造它们。您可以通过用
Stage
的前向声明替换 stuffcollection.h 中的#include "Stage.h"
来解决这个问题(即class Stage;
)。It's a circular dependency. Don't make them. This one you can resolve by replacing
#include "Stage.h"
in stuffcollection.h with a forward declaration ofStage
(i.e.class Stage;
).您可以使用前向声明,即
class Stage;
和class Stuffcollection
来代替#include
。它还具有减少依赖性的好处。Instead of the
#include
you can use forward declarations i.e.class Stage;
andclass Stuffcollection
. It also has the benefit of reducing dependencies.