1

Test2私は 2 つのテンプレート引数を取るテンプレート化された class( ) を作成しようとしていType1ますType2。2 番目の引数も、2 つのテンプレート引数 (TypeAおよびTypeB) を取るテンプレート化されたクラスになることが知られています。

ここで、 のオブジェクトを作成するためにTest2、ユーザーが 2 種類のコンストラクターのいずれかを使用できるようにしたいと考えています。

  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";}

};

上記のように使用できるように、それらをエクスポートすると便利TypeXです。TypeYTest2

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はテンプレートです。あなたは正確に何を考えTypeX、何TypeYを定義していますか?行内template<typename TypeX, typename TypeY> class Type2 >では、それらは無視されます。

于 2010-11-21T17:46:12.270 に答える
0

1 つのオプションを次に示します。

#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

1 つ目はテンプレート クラスのインスタンス化であり、パラメーターを受け入れません。2 つ目は、2 つのパラメーターを受け入れるテンプレート クラス パターンです。

于 2010-11-21T17:48:07.280 に答える