Buddy_Zhang1 发表于 2016-04-06 12:04

如何防止头文件嵌套问题

最近由于项目的头文件不断增多,以及头文件之间不断的相互引用

导致头文件嵌套......


无法找到问题的根本原因,

请问有过相关经验的朋友如何解决这类问题的........


头文件数量 200+

谢谢

_nosay 发表于 2016-04-06 19:07

回复 1# Buddy_Zhang1

我自己遇到这种情况,会用下面这两种方法:

① 把类型提前声明一下
file-a.h:
#include "file-b.h"
struct B;
struct A {
   struct B *b;
};

file-b.h:
#include "file-a.h"
struct A;
struct B {
   struct A *a;
};


② 把struct A、struct B定义在comm.h,然后:
file-a.h:
#include "comm.h"

file-b.h:
#include "comm.h"

Buddy_Zhang1 发表于 2016-04-06 21:52

回复 2# _nosay


    我的头文件都是使用这种写法的,

   
页: [1]
查看完整版本: 如何防止头文件嵌套问题