0

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,

  • 1
    Can I ask you why do you need that? – besworland May 08 '12 at 10:11
  • i need to implement association between to object using templates , here objects ares AA and BB – user1368287 May 08 '12 at 10:12
  • Such a class design is purposeless. I don't think it is useful in any sense. – Nawaz May 08 '12 at 10:18
  • This must be one of the cases where MS Dev Studio is non-compliant, because this works fine for me. – Grimm The Opiner May 08 '12 at 10:24
  • 2
    On the contrary, the code you've shown use [works as is](http://ideone.com/1bsQB). The error you have likely stems from code you *haven't* shown us where the template parameter is used as if it were complete, where it isn't in the case of `A`. (Although somehow `A` is mentioned in that error -- more code you haven't shown?) – Luc Danton May 08 '12 at 10:24
  • code added [i use g++ Debian 4.6.3-3 compiler] – user1368287 May 08 '12 at 10:37

2 Answers2

5

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:

struct A;
struct B;

struct A { B x; };

struct B { A y; };

Conceptually, this can’t work. Because, really, what you’d get here is an A that contains a B that contains an A that contains a B … 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.

template <typename T> class A { T* link;};
template <typename U> class B { U* link;};
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

Check out this question I asked some time back. How to forward declare a template class

Basically, if you are forward declaring a template type, you have to specify all the template parameters.

Community
  • 1
  • 1
nakiya
  • 14,063
  • 21
  • 79
  • 118
  • this is about template definition here the problem is with template instantiation like doing : A a; – user1368287 May 08 '12 at 10:16
  • I do not see where you instantiate the class. Anyway, you can't resolve the class definition error because template classes are not really classes - they are just templates for creating classes. What you are doing when you code `A` is making a new class - not an object. – nakiya May 08 '12 at 10:22
  • Yes A is a class thats why i wrote: [Class AA : A] ! – user1368287 May 08 '12 at 10:29