如何定义另一个模板类的内部模板类的构造函数

How to define the constructor of an inner template class of another template class?

本文关键字:内部 构造函数 另一个 何定义 定义      更新时间:2023-10-16

我有另一个模板类的内部模板类:

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

当我声明内部类构造函数时,我会得到一些错误:

//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    
{
   // ...
}

我也试过:

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

类型不完整,但构造函数没有类型。。。

我有点困在这里了。如何申报?

执行以下操作:

template<typename T1>
template<typename T2>
C1<T1>::C2<T2>::C2()
{
}

不能通过定义内部模板类的方法来专门化外部类。如果你想专门化内部类和外部类,你可以:

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

实时

如果你想保持内部类的通用性,你应该首先专门化外部类:

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

然后定义类C2、的构造函数

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

实时