2016-04-28 4 views
0

私はC++でテンプレートを使うのがかなり新しいです。 以下は私が使用しようとしているコードです。私はそれのためのオブジェクトを作成し、それで定義されたメソッドを使用する方法として把握できないので、私は次のコードを使用することができません。オブジェクトを作成できず、テンプレートクラスのメソッドを使用することができません

template <typename UInt> class nCr { 
    public: 
     typedef UInt result_type; 
     typedef UInt first_argument_type; 
     typedef UInt second_argument_type; 

     result_type operator()(first_argument_type n, second_argument_type k) { 
      if (n_ != n) { 
       n_ = n; 
       B_.resize(n); 
      } // if n 

      return B_[k]; 
     } // operator() 

    private: 
     int n_ = -1; 
     std::vector<result_type> B_; 

    }; 

そして、どのように私は、オブジェクトを作成していますが次のとおりです。このため

#include <iostream> 
#include "math.hpp" // WHere the above class nCr is defined 

int main() { 
    int n =4; 
    nCr x(4,2); 

    return 0; 
} 

私は

error: use of class template 'jaz::Binom' requires template arguments  
     nCr x(4,2);   
      ^
./include/math.hpp:68:34: note: template is declared here  
    template <typename UInt> class nCr {  
    ~~~~~~~~~~~~~~~~~~~~~~~~  ^  

として任意の提案を、エラーを作成していますか?

答えて

5

最初のエラーnCrはテンプレートクラスです。テンプレート引数には、nCr<int>のように指定する必要があります。

2番目のエラーnCr<int> x(4,2);は、2つのパラメータを取りますが、nCrにそのようなコンストラクタがないコンストラクタを使用してnCr<int>を構築することを意味します。

nCr<int> x; 

一致するコンストラクタがありませんので、nCr<int> x(4,2) // doesn't work

を:あなたは、それはテンプレートクラスであるので、引数を指定

nCr<int> x; 
int result = x(4, 2); 
0

を意味するかもしれないので、その代わりに、あなたは、nCroperator()を定義しています最初にxと宣言し、クラスで定義したoperator()を呼び出します。

nCr<int> x; 
int value = x(4,2); 
関連する問題