Resolving a Circular Dependency between Template Classes

Jon Purdy picture Jon Purdy · Jul 28, 2010 · Viewed 13.3k times · Source

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".

Answer

KeithB picture KeithB · Jul 28, 2010

What you need to do is seperate the class declarations from the implementation. So something like

template <class T> class Foo : public Base
{
    public:
    Base* convert(ID) const;
}

template <class T> class Bar : public Base
{
    public:
    Base* convert(ID) const;
}

template <class T> Base* Foo<T>::convert(ID) const {return new Bar<T>;}
template <class T> Base* Bar<T>::convert(ID) const {return new Foo<T>;}

This way, you have complete class definitions when the functions are defined.