我的教授对模板很陌生,在教它们方面很糟糕,所以我想自己学习。观看了几个视频,我似乎无法理解我做错了什么。当我取出模板时,我的整个代码都会编译,但是一旦添加代码

//将所有先前的int替换为T

template <typename T>
class Checks
{
public:
int ispair(vector<T> dvalues);
int flush(Vector<T> dsuits);
int straight(vector<T> dvalues);
int threeofakind(vector<T> dvalues);
int fourofakind(vector<T> dvalues);
int fullhouse(vector<T> dvalues);
Checks(); //didn't include all of header since there's a lot more want to save room
}


当我这样做时,我在得到0之前得到了很多错误(58)。当我尝试在另一个.cpp中使用我的Checks类时:

Checks ck1;
Checks ck2; //this would be in another class


我收到此错误:检查没有可用的适当的默认构造函数。

显然,我做模板的方式有问题,有什么建议或帮助吗?

最佳答案

您有3期


您忘记在向量前添加std ::。
一个向量被写成Vector而不是vector
您没有使用}结束课程;

#include <vector>

template <typename T>
class Checks {
public:
    int ispair(std::vector<T> dvalues);
    int flush(std::vector<T> dsuits); //was Vector
    int straight(std::vector<T> dvalues);
    int threeofakind(std::vector<T> dvalues);
    int fourofakind(std::vector<T> dvalues);
    int fullhouse(std::vector<T> dvalues);

}; //didn'T close the class with };



**编辑**

int main(int argc, char** argv) {
    Checks<int> check;
    std::vector<int> v;
    check.ispair(v);
    return EXIT_SUCCESS;
}


检查

#include <vector>

template <typename T>
class Checks {
public:
    int ispair(std::vector<T> dvalues);
};

template<class T>
int Checks<T>::ispair(std::vector<T> dvalues) {
    return 0;
}

关于c++ - header 中的模板不让我使用我的类(class)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22287007/

10-11 23:04
查看更多