C++ 将问题包含在其他文件中?(提升包括)

C++ Include problems in a different file? (Boost include)

本文关键字:包括 文件 问题 包含 其他 C++      更新时间:2023-10-16

所以我正在使用一个在里面使用 Boost 的库LibA。我有以下结构:

Class1.h:

#ifndef CLASS1_H
#define CLASS1_H
#include <LibA/major.hpp>
#include <other-useful-libraries.h>
namespace LIB{
class HelperClass{
...
};
}
#endif

Class1.cpp

#include <Class1.h>
...Implement Class1...

这两个文件编译成功。但是,我还有另一门课:

Class2.h

#ifndef CLASS2_H
#define CLASS2_H
#include <Class1.h>
#include <other-useful-libraries.h>
namespace LIB{
class MainClass{
...
};
}
#endif

Class2.cpp

#include<Class2.h>
..Implement Class2...

但是,一旦我编译了它,我就会从 Class2 中抛出大量 Boost 错误.cpp主要是未在 boost 中声明的变量、指令问题、一堆error: expected '}' before end of line以及许多其他仅指向包含中的某些问题的错误。我在这里做错了什么吗?

我知道这些错误非常抽象,但以防万一有人在将来的某个时候遇到这个问题。错误是由于包含的顺序。我将 #include 放在包含的最顶部,并编译了文件。Boost 似乎在包含(带有包含保护(的排序方面存在错误,因此请确保它在顶部。