Ehs*_*hmi 0 c++ dependencies header class include
我正在使用visual studio 2010开发一个C++程序.我有这些类定义和头文件:
sh:
class s : oe {
...
};
Run Code Online (Sandbox Code Playgroud)
th:
class t : oe {
...
};
Run Code Online (Sandbox Code Playgroud)
oe.h:
class oe {
...
o getO();//we reference to class 'o' in oe.h, so we must include o.h begore oe.h
};
Run Code Online (Sandbox Code Playgroud)
哦,哦:
class o {
...
s getS();//we reference to class 's' in o.h, so we must include s.h begore o.h
};
Run Code Online (Sandbox Code Playgroud)
问题是,我们参照"O"级的oe.h
,所以我们必须包括o.h
之前oe.h
,和我们也参考类的' o.h
,所以我们必须包括s.h
前o.h
,但我们不能这样做,因为s.h
需要oe.h
和oe.h
需求o.h
及o.h
需求s.h
!
如您所见,类依赖循环中存在某种循环,因此无法编译项目.如果我删除sh&th&oe.h之间的依赖关系,问题将解决(这是stdafx.h
针对此状态):
#include "s.h"
#include "t.h"
#include "o.h"
#include "oe.h"
Run Code Online (Sandbox Code Playgroud)
但我必须使用所有给定的依赖项,我无法删除任何依赖项.任何的想法?
您可以使用前向声明来解决此问题,并将实现移至实现文件.
而不是包含标题s
,只需向前声明它:
class s;
Run Code Online (Sandbox Code Playgroud)
并且您可以将它用作除类的数据成员之外的任何其他类型的不完整类型.(假设实现是分开的).
这很可能不会解决潜在的问题,这是你的设计.
归档时间: |
|
查看次数: |
1462 次 |
最近记录: |