循环 #include 的标头防护问题

发布于 09-02 06:42 字数 537 浏览 8 评论 0原文

我正在制作一个小型C++框架,其中包含许多.h和.cpp。

我创建了一个通用包含,其中包含我所有的 .h 文件,例如:

framework.h

#include "A.h"
#include "B.h"
#include "C.h"

每个 .h 标头都受到包含保护的保护,例如

#ifndef A_HEADER
#define A_HEADER
...
#endif

问题是,我希望能够在所有子 .h 中包含“framework.h”例如,但它会导致很多编译器错误:

#ifndef A_HEADER
#define A_HEADER

#include "framework.h"
...
#endif

如果我为每个子标头使用真正的头文件,并为使用我的框架的framework.h使用它,它就可以正常工作..

我只想将主标头包含在其中我所有的 sub .h 因此我不需要每次都包含所有依赖项。

谢谢 :)

I am making a small C++ framework, which contains many .h and .cpp.

I have created a general include which include all my .h file such as:

framework.h

#include "A.h"
#include "B.h"
#include "C.h"

each .h header are protected with include guard such as

#ifndef A_HEADER
#define A_HEADER
...
#endif

The issues is, I would like to be able to include "framework.h" inside all the sub .h such as, but it cause lots of compiler error:

#ifndef A_HEADER
#define A_HEADER

#include "framework.h"
...
#endif

If instead I use the real header file for each sub header, and the framework.h for what ever use my framework it works fine..

I would just like to include the main header inside all my sub .h so I dont need to include all the dependency everytime.

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

阳光下慵懒的猫2024-09-09 06:42:53

基本上你所做的就是在framework.h中#include "Ah"和在Ah中#include "framework.h"这会导致头文件的循环依赖,你会得到诸如未定义的类A之类的错误。要解决此问题,请在头文件中使用前向声明,并仅在相应的cpp文件中使用#include。如果这是不可能的,那么除了包含单独的头文件之外,我看不到任何其他选项。

Basically what your doing is #include "A.h" in framework.h and #include "framework.h" in A.h. This causes cyclic dependency of the header files and you will get errors such as undefined class A. To solve this, use forward declarations in header file and #include only in corresponding cpp file. If that is not possible then I don't see any other option other than including individual header files.

花间憩2024-09-09 06:42:53

您不应将主头文件包含在子头文件中。它应该用来让用户的生活更轻松,而不是你的。

相反,请执行以下操作:

1) 在相关子头文件中对所需的所有内容进行前向定义。

2) 仅在 CPP 文件中包含所需的子头文件。

3)当在应用程序代码中使用框架时(例如),您可以包含主框架头文件。

You should not including the main header file inside the sub-header files. It should be used to make user's life easier, not yours.

Instead do following:

1) Make forward definitions of all you need in the related sub-header files.

2) Include only needed sub-header files inside CPP files.

3) When using your framework inside an application code (for example), then you could include the main framework header file.

橘虞初梦2024-09-09 06:42:53

只需使用包含防护来保护主标头:

#ifndef FRAMEWORK_H
#   define FRAMEWORK_H
#   include <A.h>
#   include <B.h>
#   include <C.h>
#endif

这将防止递归包含。

Just protect the main header with include guards too:

#ifndef FRAMEWORK_H
#   define FRAMEWORK_H
#   include <A.h>
#   include <B.h>
#   include <C.h>
#endif

That will prevent recursive inclusion.

残月升风2024-09-09 06:42:53

我建议使用 #pragma Once ,并将其放在所有头文件的顶部(framework.h、Ah、Bh 和 Ch)。

不过,如果您愿意,我认为您也可以通过简单地在 Framework.h 中添加包含保护来解决您的问题。

i would recommend using #pragma once, and placing that at the top of all of your header files (framework.h, A.h, B.h, and C.h).

Although, if you'd rather, I think you could fix your problem by simply putting an include guard in framework.h as well.

不必你懂2024-09-09 06:42:53

我猜你在 B 和 C 之间存在依赖关系,使得 B 依赖于 C,但在 Framework.h 中,C 包含在 B 之后。

I guess you have a dependency between - say B and C such that B depends on C, but in framework.h C is included after B.

从来不烧饼2024-09-09 06:42:53

在 C++ 中,循环包含通常是一个坏主意。虽然使用标头保护可以防止预处理器进入无限循环(或因此引发错误),但您会遇到意外的编译器错误,因为在某些时候,如果您认为包含头文件,则不会包含头文件。

您应该包含来自 framework.hAhBhCh 以及 Ah >,不要包含framework.h,只需向前声明您从中使用的类即可。或者反过来做:包含来自 AhBhChframework.h,并转发声明framework.h 中的类。当然,将需要比例如 class A 更详细声明的所有代码放入 .cpp 文件中。

Circular includes are generally a bad idea in C++. While having header guards will prevent the preprocessor from going into infinite loop (or throwing an error because of this), you will get unexpected compiler errors, because at some point a header file will not be included when if you think it is.

You should include A.h, B.h and C.h from framework.h, and in A.h, do not include framework.h, just forward declare the classes you use from it. Or do it the other way around: include framework.h from A.h, B.h and C.h, and forward declare classes in framework.h. And, of course, put every code that would require any more detailed declarations than for example class A to the .cpp files.

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