Headers Including Each Other in C++(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:
啊
#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.
这篇关于C++ 中相互包含的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 中相互包含的头文件
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01