26

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

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
  • Cyclic dependencies are rarely a good idea. Try to refactor it so that the dependency is broken. A first idea would be moving the `convert` method into a free function that depends on both `Bar` and `Foo`... – David Rodríguez - dribeas Jul 28 '10 at 15:17

4 Answers4

24

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.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
KeithB
  • 16,577
  • 3
  • 41
  • 45
  • Took me a couple of reads to work out what you are doing with this answer - by merging the two headers together and putting the function definitions below both class declarations you avoid the problems - good thinking. Helped me too – Phil Rosenberg Dec 10 '14 at 16:54
  • 25
    @KeithB But you're doing that in the same file. How do you do it in two separate header files like in the original question? – David Doria Mar 04 '16 at 13:31
  • 3
    @DavidDoria It seems [this answer](https://stackoverflow.com/a/45403262/111307) answers that question – bobobobo Sep 28 '21 at 03:53
19

(Updated) You should be able to handle that the same as with non-template classes. Write your Bar.h like this. (And similarly for Foo.h)

#if !defined(BAR_H_INCLUDED)
#define BAR_H_INCLUDED

template <class T>
class Foo;

template <class T>
class Bar
{
    /// Declarations, no implementations.
}    

#include "Foo.h"

template <class T>
Base* Bar<T>::Convert() {  /* implementation here... */ }
#endif
James Curran
  • 101,701
  • 37
  • 181
  • 258
  • No dice. The classes can't be forward-declared because I need to make use of their members, or at least the constructor, in order to perform the conversion. I get the expected "invalid use of incomplete type". – Jon Purdy Jul 28 '10 at 14:31
  • The solution I worked out myself from KeithB's answer is similar to this, but I don't think this actually compiles because `Foo.h` and `Bar.h` would still need to include one another, so one would come up empty-handed. – Jon Purdy Jul 28 '10 at 14:58
  • @Jon: That's why I have the include guard. Bar.h includes Foo.h which attempts to include Bar.h, but gets nothing the second time. – James Curran Jul 28 '10 at 16:09
  • @JamesCurran what if both class are inside namespace? – Moia Apr 20 '18 at 06:31
  • @Moia Wrap both the declaration part and implementation part with two separate namespaces and leave the include statement in the middle. Should be something like namespace xxx {...declaration...} #include "header" namespace {...implementation...} – ph3rin Apr 08 '19 at 08:55
13

You should use template class forward declarations in either headers

template <class T>
class X;

is perfectly good template class forward declaration.

Mazyod
  • 22,319
  • 10
  • 92
  • 157
ULysses
  • 978
  • 4
  • 9
11

James Curran's answer is a godsend. Generally speaking, James' idea is to restrict inclusion of required header files until the moment the members(' declarations) coming from included header files are needed. As an example:

t1.hh

#ifndef S_SIGNATURE
#define S_SIGNATURE

struct G; // forward declaration

template<typename T>
struct S {
  void s_method(G &);
};

#include "t2.hh" // now we only need G's member declarations

template<typename T>
void S<T>::s_method(G&g) { g.g_method(*this); }

#endif

t2.hh

#ifndef G_SIGNATURE
#define G_SIGNATURE

template<typename T>
struct S; // forward declaration

struct G {
  template<typename T>
  void g_method(S<T>&);
};

#include "t1.hh" // now we only need S' member declarations

template<typename T>
void G::g_method(S<T>& s) { s.s_method(*this); }

#endif

t.cc

#include "t1.hh"
#include "t2.hh"

S<int> s;
G g;

int main(int argc,char**argv) {
  g.g_method(s); // instantiation of G::g_method<int>(S<int>&)
}
Vijay Rao
  • 111
  • 1
  • 2