I have two classes, Foo<T>
and Bar<T>
, derived from Base
. Each overrides a method virtual Base* convert(ID) const
, where ID
is an instance of a type that uniquely identifies a particular instantiation of Foo
or Bar
(pretend it's an enum
). The problem is that Foo::convert()
needs to be able to return a Bar
instance, and likewise Bar::convert()
needs to be able to instantiate Foo
. Since they're both templates, this results in a circular dependency between Foo.h
and Bar.h
. How do I resolve this?
Edit: A forward declaration does not work because the implementation of each method needs the constructor of the other class:
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.
...
}
The error is, naturally, "invalid use of incomplete type".