结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配

constructor error for array of structure, error msg: no instance of constructor matches the argument list

本文关键字:构造函数 错误 实例 参数 列表 数组 消息 结构      更新时间:2023-10-16
Person* studentList[5];
studentList[0] = new Student("Jane", 1);
studentList[1] = new Student("Jim", 2);
studentList[2] = new Student("Jacques", 3);
studentList[3] = new Student("Juan", 4);
studentList[4] = new Student("Junlian", 5);

学生是Person的子结构,最后5行显示错误没有构造函数Student::Student的实例匹配参数列表,我无法弄清楚问题。 下面是构造函数:

Student::Student(char * na, int nm) {
this->name = na;
this->number = nm;
}

如果有人能帮助解释,我将不胜感激。

C++ 中的字符串文字(与 C 不同(const char*。它们不能转换为非常量char*。要编译程序,您需要将构造函数签名更改为

Student::Student(const char* na, int nm)

您还需要确保nameStudent中声明为const char*