我有两个类, Foo<T>
和 Bar<T>
,派生自 Base
. 每个都覆盖一个方法 virtual Base* convert(ID) const
,其中 ID
是一个唯一标识 Foo
或 Bar
(假装它是 enum
)的特定实例的类型的实例 . 问题是 Foo::convert()
需要能够返回 Bar
实例,同样 Bar::convert()
需要能够实例化 Foo
. 由于它们都是模板,因此导致 Foo.h
和 Bar.h
之间存在循环依赖关系 . 我该如何解决这个问题?
Edit: 前向声明不起作用,因为每个方法的实现需要另一个类的构造函数:
Foo.h
:
#include <Base.h>
template<class T> class Bar;
template<class T>
class Foo : public Base { ... };
template<class T>
Base* Foo<T>::convert(ID id) const {
if (id == BAR_INT)
return new Bar<int>(value); // Error.
...
}
Bar.h
:
#include <Base.h>
template<class T> class Foo;
template<class T>
class Bar : public Base { ... };
template<class T>
Base* Bar<T>::convert(ID id) const {
if (id == FOO_FLOAT)
return new Foo<float>(value); // Error.
...
}
该错误自然是“无效使用不完整类型” .
4 回答
您需要做的是从实现中分离类声明 . 所以像
这样,在定义函数时,您就拥有了完整的类定义 .
您应该在任一标头中使用模板类前向声明
是完美的模板类前向声明 .
(更新)您应该能够处理与非模板类相同的操作 . 像这样写你的Bar.h. (同样对于Foo.h)
James Curran的回答是天赐之物 . 一般来说,James的想法是限制所需头文件的包含,直到需要来自包含头文件的成员('声明)为止 . 举个例子:
t1.hh
t2.hh
t.cc