0

I have a very simple problem - but I don't find any solution. I want to use a template class two times, but with mutual dependency in the template argument.

I want to write something like this:

template<class T> class X
{
};

class B;
using A = X<B*>;
using B = X<A*>;

My Problem is, that I need in the defintion of A the type B, but for the defintion of B I need the type A. And a forward declaration like "class B" doesn't work.

Microsoft Visual Studio 2017 says: error C2371: "B": new definition; different base types.

What I need is something like "type B;" where I can tell the compiler, that B is a type. For a pointer it doesn't matter which type it is.

1 Answers1

1

Using does not define a class, that's why you can't forward it. Using defines more or less a placeholder.

You could just go for wrapper classes and forward them:

template<class T> class X
{
};

struct B;
struct A : public X<B*> {};
struct B : public X<A*> {};
Zacharias
  • 576
  • 3
  • 6
  • Thank you. That's what I make today - but my classes are not empty - they contains the using for inheriting the constructors. And I look for a solution without duplicating code. – Ernst H. Ernesti Jul 18 '18 at 09:05
  • Take a look at https://stackoverflow.com/questions/347358/inheriting-constructors – Zacharias Jul 18 '18 at 11:40