我一直在尝试创建一个带有Test2
2 个模板参数的模板类(),Type1
并且Type2
. 众所周知,第二个参数也是一个模板类,它接受 2 个模板参数(TypeA
和TypeB
)。
现在,为了构造 的对象Test2
,我希望用户能够使用两种类型的构造函数中的任何一种:
- 一个接受
Type1
和的对象Type2
。 - 一个接受
Type1
和TypeA
的对象TypeB
。
我写了以下代码:
#include <iostream>
template<class TypeA, class TypeB>
struct Test
{
TypeA t1obj;
TypeB t2obj;
Test(const TypeA& t1, const TypeB& t2)
: t1obj(t1), t2obj(t2) {std::cout<<"Test::Type1, Type2\n";}
};
template<class Type1,
template<typename TypeX, typename TypeY> class Type2 >
struct Test2
{
Type1 t1obj;
Type2<typename TypeX, typename TypeY> t2obj; //Line 17
Test2(const Type1& t1,
const Type2<typename TypeX, typename TypeY>& t2) //Line 20
: t1obj(t1), t2obj(t2) { std::cout<<"Test2::Type1, Type2\n";}
Test2(const Type1& t1,
const TypeX& x,
const TypeY& y)
: t1obj(t1), t2obj(x,y) { std::cout<<"Test2::Type1, X, Y\n";}
};
int main()
{
Test<int, char> obj1(1,'a');
Test2<int, Test<int, char> > strangeobj1(10,obj1);
Test2<int, Test<int, char> > strangeobj2(1,2,'b');
}
我已经尝试了很多,但我得到了非常荒谬的错误,例如:
wrong number of template arguments (1, should be 2)
在 17 号线和 20 号线。