简体   繁体   English

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

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

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? 如何申报?

Perform the following: 执行以下操作:

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

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, 然后定义C2的构造函数,

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

LIVE 生活

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM