gpt4 book ai didi

c++ - 结构数组的构造函数错误,错误消息 : no instance of constructor matches the argument list

转载 作者:行者123 更新时间:2023-11-28 01:29:31 24 4
gpt4 key购买 nike

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);

Student 是 Person 的子结构,最后 5 行显示错误 no instance of constructor::Student matches the argument list,我想不通这个问题。这是构造函数:

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*

关于c++ - 结构数组的构造函数错误,错误消息 : no instance of constructor matches the argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52231682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com