【发布时间】:2012-06-29 19:49:33
【问题描述】:
我正在使用 Visual Studio 2010 开发 C++ 程序。我有这些类定义和头文件:
s.h:
class s : oe {
...
};
t.h:
class t : oe {
...
};
oe.h:
class oe {
...
o getO();//we reference to class 'o' in oe.h, so we must include o.h begore oe.h
};
&哦:
class o {
...
s getS();//we reference to class 's' in o.h, so we must include s.h begore o.h
};
问题是我们在oe.h中引用了类'o',所以我们必须在oe.h之前包含o.h,而且我们在o.h中引用了类's',所以我们必须包含@ 987654330@ 在o.h 之前,但我们不能这样做,因为s.h 需要oe.h 和oe.h 需要o.h 和o.h 需要s.h !
如您所见,类依赖循环中存在某种循环,因此我无法编译该项目。如果我删除 s.h & t.h & oe.h 之间的依赖关系,问题就会解决(这里是 stdafx.h 表示这个状态):
#include "s.h"
#include "t.h"
#include "o.h"
#include "oe.h"
但是我必须使用所有给定的依赖项并且我不能删除任何依赖项。有什么想法吗?
【问题讨论】:
-
搜索前向声明和循环头依赖。在 stackoverflow 上有很多关于这个的问题。
标签: c++ class header include dependencies