C++ Header Guard问题

JP.*_*JP. 2 c++ header include-guards include

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

我创建了一个包含所有.h文件的常规包含,例如:

framework.h

#include "A.h"
#include "B.h"
#include "C.h"
Run Code Online (Sandbox Code Playgroud)

每个.h标题都受到包含保护的保护

#ifndef A_HEADER
#define A_HEADER
...
#endif
Run Code Online (Sandbox Code Playgroud)

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

#ifndef A_HEADER
#define A_HEADER

#include "framework.h"
...
#endif
Run Code Online (Sandbox Code Playgroud)

如果相反,我使用每个子标头的真实头文件,而framework.h用于什么使用我的框架它工作正常..

我只想在所有sub .h中包含主标题,所以我不需要每次都包含所有依赖项.

谢谢 :)

Nav*_*een 7

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