双包含解决方案?

发布于 2024-12-08 14:54:46 字数 1187 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2024-12-15 14:54:46

您有一个循环依赖。而是在 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

基本原理:
您可以在上面的代码片段中使用前向声明,因为 Stuffcollection.h 仅使用指向 Stage 的指针。

说明:
使用类 Stage 的前向声明,编译器不知道它的组成,也不知道它内部的成员,编译器只知道 Stage 是一个 type< /代码>。因此,
Stage 是编译器的不完整类型。对于不完整类型,我们无法创建它的对象或执行任何需要编译器了解 Stage 布局的操作,或者除了 Stage 只是一种类型这一事实之外。由于指向所有对象的指针只需要相同的内存分配,因此当仅将 Stage 引用为指针时,您可以使用前向声明。

您可以使用前向声明来克服循环依赖问题。

进一步阅读:
何时使用前向声明?

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?

南…巷孤猫 2024-12-15 14:54:46

这是一个循环依赖。不要制造它们。您可以通过用 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 of Stage (i.e. class Stage;).

与之呼应 2024-12-15 14:54:46

您可以使用前向声明,即 class Stage;class Stuffcollection 来代替 #include。它还具有减少依赖性的好处。

Instead of the #include you can use forward declarations i.e. class Stage; and class Stuffcollection. It also has the benefit of reducing dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文