1

我一直在尝试创建一个带有Test22 个模板参数的模板类(),Type1并且Type2. 众所周知,第二个参数也是一个模板类,它接受 2 个模板参数(TypeATypeB)。

现在,为了构造 的对象Test2,我希望用户能够使用两种类型的构造函数中的任何一种:

  1. 一个接受 Type1和的对象Type2
  2. 一个接受Type1TypeA的对象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 号线。

4

5 回答 5

6

它不是那样工作的。Test<int, char>是一个成熟的类型,而不是一个模板。所以你需要类型参数

template<class Type1,
         class Type2 >
struct Test2
{
    Type1 t1obj;
    Type2 t2obj; //Line 17

    Test2(const Type1& t1,
          const Type2& t2) //Line 20
        : t1obj(t1), t2obj(t2) { std::cout<<"Test2::Type1, Type2\n";}

    Test2(const Type1& t1,
          const typename Type2::a_type& x,
          const typename Type2::b_type& y)
        : t1obj(t1), t2obj(x,y) { std::cout<<"Test2::Type1, X, Y\n";}

};

对于获取TypeXTypeY导出它们很有用,因此您可以Test2如上所示使用它们。

template<class TypeA, class TypeB>
struct Test
{
    typedef TypeA a_type;
    typedef TypeB b_type;

    // and using them, to show their meaning
    a_type t1obj;
    b_type t2obj;

    Test(const a_type& t1, const b_type& t2)
        : t1obj(t1), t2obj(t2) {std::cout<<"Test::Type1, Type2\n";}
};

请务必阅读在从属名称上放置“模板”和“类型名称”的位置,以了解为什么以及何时typename在上述类型名称之前使用。

于 2010-11-21T17:44:55.257 回答
1

这有几个错误,但主要错误似乎是

Test2<int, Test<int, char> >

不是您传递模板模板参数的方式。这将通过使用

Test2<int, Test>

那是因为Test它是一个模板,但Test<int, char>它是一个类型(从该模板生成。)

于 2010-11-21T17:47:46.793 回答
0

Type1是一个类型,Type2是一个模板。你到底是怎么想的TypeXTypeY被定义的?在 line 内template<typename TypeX, typename TypeY> class Type2 >,它们被忽略。

于 2010-11-21T17:46:12.270 回答
0

这是一个选项:

#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, typename TypeX, typename TypeY,
         template <typename TypeXi, typename TypeYi> 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, int, char, Test> strangeobj1(10,obj1);
    Test2<int, int, char, Test> strangeobj2(1,2,'b');

}
于 2010-11-21T17:47:00.510 回答
0

Test<int, char>不匹配template<typename TypeX, typename TypeY> class Type2

第一个是模板类的实例化,它不接受任何参数。第二个是接受两个参数的模板类模式。

于 2010-11-21T17:48:07.280 回答