C++ 中相互包含的标头
我是 C++ 新手,但我无法在网上找到这个(很可能是微不足道的)问题的答案.我在编译一些两个类相互包含的代码时遇到了一些麻烦.首先,我的#include 语句应该放在宏的内部还是外部?在实践中,这似乎并不重要.但是,在这种特殊情况下,我遇到了麻烦.将#include 语句放在宏之外会导致编译器递归并给我#include 嵌套太深"的错误.这对我来说似乎很有意义,因为在调用 #include 之前没有完全定义任何类.然而,奇怪的是,当我尝试将它们放入其中时,我无法声明其中一个类的类型,因为它无法识别.本质上,这就是我要编译的内容:
I'm a C++ newbie, but I wasn't able to find the answer to this (most likely trivial) question online. I am having some trouble compiling some code where two classes include each other. To begin, should my #include statements go inside or outside of my macros? In practice, this hasn't seemed to matter. However, in this particular case, I am having trouble. Putting the #include statements outside of the macros causes the compiler to recurse and gives me "#include nested too deeply" errors. This seems to makes sense to me since neither class has been fully defined before #include has been invoked. However, strangely, when I try to put them inside, I am unable to declare a type of one of the classes, for it is not recognized. Here is, in essence, what I'm trying to compile:
啊.h
#ifndef A_H_
#define A_H_
#include "B.h"
class A
{
private:
B b;
public:
A() : b(*this) {}
};
#endif /*A_H_*/
B.h
#ifndef B_H_
#define B_H_
#include "A.h"
class B
{
private:
A& a;
public:
B(A& a) : a(a) {}
};
#endif /*B_H_*/
main.cpp
#include "A.h"
int main()
{
A a;
}
如果有什么不同,我使用的是 g++ 4.3.2.
If it makes a difference, I am using g++ 4.3.2.
为了清楚起见,一般来说,#include 语句应该放在哪里?我一直看到它们超出了宏的范围,但我清楚地描述的场景似乎打破了这个原则.提前感谢任何帮助者!如果我犯了任何愚蠢的错误,请允许我澄清我的意图!
And just to be clear, in general, where should #include statements go? I have always seen them go outside of the macros, but the scenario I described clearly seems to break this principle. Thanks to any helpers in advance! Please allow me to clarify my intent if I have made any silly mistakes!
推荐答案
我认为宏"是指#ifndef 包含警卫?如果是这样,#includes 肯定应该进去.这是包含守卫存在的主要原因之一,因为否则您很容易像您注意到的那样以无限递归结束.
By "the macros" I assume you mean the #ifndef include guards? If so, #includes should definitely go inside. This is one of the major reasons why include guards exists, because otherwise you easily end up with an infinite recursion as you noticed.
无论如何,问题是在您使用 A 和 B 类(在另一个类中)时,它们还没有被声明.看看#includes 被处理后的代码是什么样子的:
Anyway, the problem is that at the time you use the A and B classes (inside the other class), they have not yet been declared. Look at what the code looks like after the #includes have been processed:
//#include "A.h" start
#ifndef A_H_
#define A_H_
//#include "B.h" start
#ifndef B_H_
#define B_H_
//#include "A.h" start
#ifndef A_H_ // A_H_ is already defined, so the contents of the file are skipped at this point
#endif /*A_H_*/
//#include "A.h" end
class B
{
private:
A& a;
public:
B(A& a) : a(a) {}
};
#endif /*B_H_*/
//#include "B.h" end
class A
{
private:
B b;
public:
A() : b(*this) {}
};
#endif /*A_H_*/
//#include "A.h" end
int main()
{
A a;
}
现在阅读代码.B 是编译器遇到的第一个类,它包含一个 A&
成员.A
是什么?编译器还没有遇到任何A
的定义,所以它会发出错误.
Now read the code. B is the first class the compiler encounters, and it includes an A&
member. What is A
? The compiler hasn't encountered any definition of A
yet, so it issues an error.
解决方法是对A进行前向声明.在B的定义之前的某个时刻,添加一行class A;
The solution is to make a forward declaration of A. At some point before the definition of B, add a line class A;
这为编译器提供了必要的信息,即 A 是一个类.我们对此一无所知,但由于 B 只需要包含对它的引用,这就足够了.在 A 的定义中,我们需要 B 类型的成员(不是引用),所以这里 B 的整个定义必须是可见的.幸运的是,它是.
This gives the compiler the necessary information, that A is a class. We don't know anything else about it yet, but since B only needs to include a reference to it, this is good enough. In the definition of A, we need a member of type B (not a reference), so here the entire definition of B has to be visible. Which it is, luckily.
相关文章