c++ - C++ 中的相互包含 .. 它是如何工作的?

标签 c++ include header-files

Possible Duplicate:
Proper way to #include when there is a circular dependency?

我对 C++ 很陌生,并且在标题中提出了问题。或者更准确地说:如果 A.h 包含 B.h 并且 B.h 包含 A.h,我会收到一条错误消息,因为“include# file "C:...\A.h"包含自身”。文件:B.h

我找不到解决此问题的方法,而我的一般设置几乎需要这些类之间的关系。 有没有可能使这项工作?

最佳答案

在头文件中使用 Include 保护。 http://en.wikipedia.org/wiki/Include_guard

#ifndef MYHEADER_H
#define MYHEADER_H

//add decls here 

#endif

如果你的头文件被多次包含,编译器会忽略它们。

根据经验,如果您包含具有 A.h 的 B.h,最好在您的应用程序中包含 A.h 和 B.h,而不是依赖于 B.h 的包含。

也只将声明放在头文件中。

不惜一切代价避免在头文件中定义。

关于c++ - C++ 中的相互包含 .. 它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6790781/

相关文章:

c++ - 从 dtor 安全地抛出异常

c++ - 编译命令eclipse c++

c++ - Visual Studio Code : C/C++: Multiple definitions for a header/include file shown; how to fix?

c++ - 对 Class::Class() 和 Class::function() 的 undefined reference

c++ - 你如何获得你的系统在 c/c++ 中使用的编码类型?

c++ - 在 C/C++ 窗口中终止进程

visual-studio-2013 - T4 包括安装 VS2013 更新 3 后破坏的指令

javascript - 不能在 AppFog Node App 上包含脚本/css

c - libwebsockets.h - 没有这样的文件或目录,Ubuntu,arm-linux-gcc 交叉编译器

c++ - 暂停后无法恢复流程