c++错误没有构造函数的实例

c++ error no instance of constructor

本文关键字:实例 构造函数 错误 c++      更新时间:2023-10-16

我是C++的新手,只想返回通过构造函数传递的值,我不确定我在下面的代码上做错了什么,它一直给我一个错误:no instance of constructor..matchescannot 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