代码之家  ›  专栏  ›  技术社区  ›  Saurabh Manchanda

在使用模板参数时出现荒谬的错误

  •  1
  • Saurabh Manchanda  · 技术社区  · 15 年前

    我一直在尝试创建一个模板类( Test2 )需要2个模板参数, Type1 和 Type2 . 众所周知,第二个参数也是一个模板类,它接受两个模板参数( TypeA 和 TypeB ).

    现在,为了构造一个 测试2 ,我希望用户能够使用以下两种类型的构造函数之一:

    1. 拿走物体的人 类型1 和 类型2 .
    2. 拿走物体的人 类型1 , A型 和 B型 .

    我写了以下代码:

    #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号线。

    5 回复  |  直到 15 年前
        1
  •  6
  •   Community Mohan Dere    9 年前

    不是这样的。 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";}
    
    };
    

    为了得到 TypeX 和 TypeY 导出它们很有用,这样您就可以在 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";}
    };
    

    一定要读 Where to put the "template" and "typename" on dependent names 了解使用的原因和时间 typename 在键入上述名称之前。

        2
  •  1
  •   sbi    15 年前

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

    Test2<int, Test<int, char> >
    

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

    Test2<int, Test>
    

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

        3
  •  0
  •   fredoverflow    15 年前

    Type1 是一种类型, Type2 是一个模板。你到底怎么想 TypeX 和 TypeY 有定义吗?内线 template<typename TypeX, typename TypeY> class Type2 > ,它们被忽略。

        4
  •  0
  •   Reinderien    15 年前

    这里有一个选择:

    #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');
    
    }
    
        5
  •  0
  •   Ben Voigt    15 年前

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

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