2010-11-21 6 views
1

テンプレートの2つの引数、Type1およびType2が必要なテンプレートクラス(Test2)を作成しようとしています。 2番目の引数は2つのテンプレート引数(TypeATypeB)をとるテンプレート化されたクラスであることも知られています。 Type1Type2のオブジェクトを取るテンプレートテンプレートの引数を使用してばかげたエラーを取得する

  1. ワン:

    は今、Test2のオブジェクトを構築するために、私は、ユーザーがコンストラクタの2種類のいずれかを使用することができるようにしたいです。

  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'); 
    
    } 
    

は、私は次のコードを書いたライン17上の

wrong number of template arguments (1, should be 2)と20

+1

_first_エラーメッセージを必ず投稿してください。残りは偽であるかもしれません。コード内にその行を示してください。 – sbi

+0

@sbi:私が言及したエラーは私が得る最初のエラーです。 –

+0

あなたの説明からは分かりませんでしたので、私はそれに言及すると思いました。 – sbi

答えて

6

それはdoesnのそのような仕事。 Test<int, char>テンプレートの代わりにタイプのフルブローンです。ですから、TypeXTypeYを取得するための型パラメータ

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

}; 

を必要とすることは、上記のように、あなたが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を使用するために理解してWhere to put the "template" and "typename" on dependent namesを必ずお読みください。

+0

しかし、テンプレートテンプレート引数は、Type2に渡された引数をキャッチするためにここでは機能しませんか?私が言及したそのエラーの理由を説明できますか? –

+0

私はC++ 0xのバリデーショナルテンプレートはここではかなり便利だと思います。 'Test2'は' Type2'のコンストラクタに必要な引数について何も知る必要はありません。 –

+0

@Ben correct。固定名付きtypedefに頼ることなく、テンプレート引数を自動的に推論する 'Test2'の部分的な特殊化を書くことができます。 –

0

Type1は、Type2がテンプレートです。 TypeXTypeYが正確に何と定義されていますか?行内のtemplate<typename TypeX, typename TypeY> class Type2 >は無視されます。

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'); 

} 
+0

'Type2 'ではなく、 'Type2 'です。 'TypeX'と' TypeY'は従属名ではありません。おそらく –

+0

。コンパイラは気にしないようです。 – Reinderien

+0

@Ben http://stackoverflow.com/questions/4231502/typename-template-and-dependent-namesを参照してください。彼らは非修飾名に依存しています。そのため、構文上無効です。そのFAQに関するご意見をお聞かせください。 –

1

が、主なエラーは、テンプレートテンプレートパラメータを渡す方法

Test2<int, Test<int, char> > 

ではないことのようです。これはTestテンプレートあるがTest<int, char>タイプからである

Test2<int, Test> 

使用して渡される(そのテンプレートから生成された。)

0

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

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

関連する問題