I want to know if is it possible in C++ to do that :
template <typename T> class A { T link;};
template <typename U> class B { U link;};
class AA : A<BB> {};
class BB : B<AA> {};
because it generates the error :
error: ‘BB’ was not declared in this scope
error: template argument 1 is invalid
i have tryed to use anticipate declaration :
class AA;
class BB;
class AA : A<BB> {};
class BB : B<AA> {};
but it didn’t work :
In instantiation of ‘A<AA>’:
error: ‘A<T>::s’ has incomplete type
error: forward declaration of ‘struct AA’
thank you for your help,
Your problem isn’t the templates, it’s the infinite nesting (and yes, technically from using incomplete types to define members). Remove the templates and you’ll get the same issue:
Conceptually, this can’t work. Because, really, what you’d get here is an
Athat contains aBthat contains anAthat contains aB… to infinity. Turtles all the way down.What does work, however, is using pointer members instead. Those work with incomplete types, and consequently the code works – and even with templates.