模板头不允许我使用我的类
template in header not letting me use my class?
对模板很陌生,我的教授教得很糟糕,所以我想自己学习。看了几个视频,我似乎不明白我做错了什么。当我取出模板时,我的整个代码编译但一旦我添加代码
//将之前所有的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;
}
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- std::带有自定义缓冲区的 iostream 不允许我写入
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?
- 错误:从"int"到"int*"的转换无效[-允许].我在下面提供了我的代码,我
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 是否允许调用方对我的 Builder 类使用任何指针类型(包括智能指针)?
- 如何创建一个接口,允许我访问C++中的按钮(和其他ui)函数,该函数是使用python中的MFC实现的
- Bazel 允许我包含来自全局安装库的标头
- 新版本的 SDL 不允许我使用SDL_image
- 为什么 C 允许我调用未声明的函数?
- 如何允许我的程序成功读取数字包含的文件
- 我不允许更改变量的声明,我可以编辑哪些其他方法
- C++概念是否允许我的类在声明/定义中指定它满足某些概念?
- 如何允许我的应用程序C 请求ICMPV4?Windows 8.1
- G++ 不允许我定义名为 "major" 的成员函数
- 对我是否需要一个哨兵值来终止我的 do while 循环以允许多个条目感到困惑
- 我的库中有多个类:不允许使用不完整的类型
- C++11 标准中的哪些条款允许我消除下面“A::operator-()”中“返回”语句中的“A”
- 错误:不允许指向不完整类类型的指针.我该怎么做
- 是什么导致了我的错误?我试图创建一个球员类和存储球员的名字作为一个变量