模板头不允许我使用我的类

template in header not letting me use my class?

本文关键字:我的 允许我 不允许      更新时间:2023-10-16

对模板很陌生,我的教授教得很糟糕,所以我想自己学习。看了几个视频,我似乎不明白我做错了什么。当我取出模板时,我的整个代码编译但一旦我添加代码

//将之前所有的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
}

当我这样做时,我得到了一堆错误(58),而之前我有0。当我试图使用我的检查类在另一个。cpp:

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

我得到这个错误:检查没有合适的默认构造函数可用。

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

只是猜测,因为我不精通CPP,但是您在定义变量时,需要指定类的类型:

Checks<int> ck1;

你有三个问题

  • 你忘了在vector前加上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 };
    

** EDIT **

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

Check.h

#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;
}
相关文章: