无法创建对象并使用其模板类的方法 我在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_; }; 模板类nCr{ 公众: 类型定义单元结果\u类型; 类型定义单元第一个参数类型; 类型定义单元第二个参数类型; 结果类型运算符()(第一个参数类型n,第二个参数类型k){ 如果(n_!=n){ n=n; B.调整大小(n); }//如果n 返回B_k[k]; }//运算符() 私人: int n_u=-1; std::向量B_; };

无法创建对象并使用其模板类的方法 我在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_; }; 模板类nCr{ 公众: 类型定义单元结果\u类型; 类型定义单元第一个参数类型; 类型定义单元第二个参数类型; 结果类型运算符()(第一个参数类型n,第二个参数类型k){ 如果(n_!=n){ n=n; B.调整大小(n); }//如果n 返回B_k[k]; }//运算符() 私人: int n_u=-1; std::向量B_; };,c++,C++,我是如何创建对象的: #include <iostream> #include "math.hpp" // WHere the above class nCr is defined int main() { int n =4; nCr x(4,2); return 0; } #包括 #包括“math.hpp”//其中定义了上述nCr类 int main(){ int n=4; nCr x(4,2); 返回0; } 为此,我创建了如下错误: error:

我是如何创建对象的:

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

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

    return 0;
}
#包括
#包括“math.hpp”//其中定义了上述nCr类
int main(){
int n=4;
nCr x(4,2);
返回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 {       
  ~~~~~~~~~~~~~~~~~~~~~~~~       ^        
错误:使用类模板“jaz::Binom”需要模板参数
nCr x(4,2);
^
./include/math.hpp:68:34:注意:此处声明了模板
模板类nCr{
~~~~~~~~~~~~~~~~~~~~~~~~       ^        

有什么建议吗?

第一个错误,
nCr
是一个模板类,您需要在提到它时指定模板参数,例如
nCr

第二个错误,
nCr x(4,2);
意味着通过它的构造函数构造一个
nCr
,这个构造函数有两个参数,但是
nCr
没有这样的构造函数。相反,你在
nCr
中定义了
操作符()
,所以你的意思可能是

nCr<int> x;
int result = x(4, 2);
ncrx;
int结果=x(4,2);

第一个错误,
nCr
是一个模板类,您需要在提到它时指定模板参数,例如
nCr

第二个错误,
nCr x(4,2);
意味着通过它的构造函数构造一个
nCr
,这个构造函数有两个参数,但是
nCr
没有这样的构造函数。相反,你在
nCr
中定义了
操作符()
,所以你的意思可能是

nCr<int> x;
int result = x(4, 2);
ncrx;
int结果=x(4,2);

由于它是一个模板类,请指定参数:

nCr<int> x;

由于它是一个模板类,请指定参数:

nCr<int> x;