1

I have an inner template class of another template class:

// hpp file
template< class T1 > class C1
{
   // ...
public:
   // ...
   C1();
   template< class T2 > C2
   {
      // ...
      C2();
   };
};

When I declare the inner class constructor I get some errors:

//cpp file
template<> C1< MyType >::C1()
{
   // ...
}

template<> template< class T2 > C1< MyType >::C2::C2() // error: invalid use of template-name ‘class C1<MyType>::C2’ without an argument list    
{
   // ...
}

I have also tried :

template<> template< class T2 > C1< MyType >::C2<T2>::C2() // error: invalid use of incomplete type ‘class C1<MyType>::C2<T2>’
{
   // ...
}

Incomplete type, but constructor has no type...

I am a little stuck here. How to declare it?

sop
  • 3,445
  • 8
  • 41
  • 84

2 Answers2

2

Perform the following:

template<typename T1>
template<typename T2>
C1<T1>::C2<T2>::C2()
{
}
James Adkison
  • 9,412
  • 2
  • 29
  • 43
1

You can't specialize the outer class by defining an inner template class's method. If you want to specialize both the inner class and outer class, you can:

template<>
template<> 
C1<MyType>::C2<char>::C2()
{
   // ...
}

LIVE

If you want to keep the inner class generic, you should specialize the outer class first:

template<> 
class C1<MyType>
{
   // ...
public:
   // ...
   C1();
   template< class T2 > class C2
   {
      public:
      // ...
      C2();
   };
};

and then define the constructor of C2 like,

template<class T2> 
C1<MyType>::C2<T2>::C2()
{
   // ...
}

LIVE

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Well I have another question then: what does `template< class T = MyClass > class C1 {};` means? Isn't it a specialization? – sop Oct 28 '15 at 08:01
  • 1
    @sop It's another definition of `C1`, so you'll get a redefinition error. `class T = MyClass` means `MyClass` is the default template parameter, so you can use it like `C1<> c1`, but it's not specialization. – songyuanyao Oct 28 '15 at 08:12