gpt4 book ai didi

C++ Header Guard 问题

转载 作者:太空狗 更新时间:2023-10-29 23:38:01 25 4
gpt4 key购买 nike

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

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

framework.h

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

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

#ifndef A_HEADER
#define A_HEADER
...
#endif

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

#ifndef A_HEADER
#define A_HEADER

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

如果相反,我为每个子头文件使用真正的头文件,而 framework.h 用于任何使用我的框架的东西,它工作正常..

我只想在所有子 .h 中包含主 header ,这样我就不需要每次都包含所有依赖项。

谢谢:)

最佳答案

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

关于C++ Header Guard 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2866539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com