c++错误没有构造函数的实例
c++ error no instance of constructor
我是C++的新手,只想返回通过构造函数传递的值,我不确定我在下面的代码上做错了什么,它一直给我一个错误:no instance of constructor..matches
,cannot convert parameter 3 from 'const char [5]' to 'int'
#include <iostream>
#include <string>
using namespace std;
class TestClass{
string a;
string b;
int x;
public:
TestClass(string m, string n, int y){
this->a = m;
this->b = n;
this->x = y;
}
int test(){
return this->x;
}
};
int main(){
TestClass *a = new TestClass("test1","test2","9999");
cout<<a->test()<<endl;
}
您将数字9999作为"9999"
传递——它周围的引号表示它是一个字符串。只需将其作为9999
传递即可。
您必须将第三个参数从"9999"更改为9999。引号表示您将其视为一个字符串,而实际上构造函数正在期待一个int
。
相关文章:
- 对象实例化调用构造函数的次数太多
- 如何使用非默认构造函数实例化模板化类
- 模板化类构造函数的模板实例化
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- C++初始化类实例时隐式调用类成员的构造函数
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++ 常量正确性/缺少支持常量和非常量实例的类的常量构造函数
- C++ - 使用另一个类的构造函数实例化一个对象
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- C++如何在类构造函数中实例化 std::array?
- 避免通过操作从私有构造函数间接实例化
- 类构造函数 - 没有构造函数的实例
- 没有构造函数模型的实例::模型匹配参数列表
- 是否允许使用初始值设定项列表将const数组引用实例化为构造函数参数
- C++:如何在结构中定义类实例.类具有参数化的构造函数
- 在保证复制的世界中构造函数实例化
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
- C++构造函数的实例都与参数列表匹配
- 为什么GCC将对全局实例构造函数的调用放在不同的部分(取决于目标)
- 如果使单一实例构造函数受到保护,缺点是什么 - 继承 - C++11.