C++ 结构数组的构造函数错误,错误消息:没有与参数列表匹配的构造函数实例
Student是Person的子结构,最后5行显示错误:constructor Student::Student的实例与参数列表不匹配,我无法找出问题所在。 以下是构造函数:C++ 结构数组的构造函数错误,错误消息:没有与参数列表匹配的构造函数实例,c++,constructor,compiler-errors,C++,Constructor,Compiler Errors,Student是Person的子结构,最后5行显示错误:constructor Student::Student的实例与参数列表不匹配,我无法找出问题所在。 以下是构造函数: Person* studentList[5]; studentList[0] = new Student("Jane", 1); studentList[1] = new Student("Jim", 2); studentList[2] = new Student("Jacques", 3); studentList[3
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);
如果有人能解释的话,我会很感激。 C++中的字符串文字(与C不同)是<代码> const char */COD>。它们不能转换为非常量
char*
。要编译程序,您需要将构造函数签名更改为
Student::Student(char * na, int nm) {
this->name = na;
this->number = nm;
}
您还需要确保<代码>名称>代码>在<代码>学生< /代码>中声明为代码> conchch**/COD>。C++中的< /p> 字符串文字(与C不同)是“代码> const char */COD>。它们不能转换为非常量char*
。要编译程序,您需要将构造函数签名更改为
Student::Student(char * na, int nm) {
this->name = na;
this->number = nm;
}
您还需要确保
name
在Student
中声明为const char*
一个关于字符数组字符串指针的警告:如果您错误地存储了一个自动(又称局部)变量,则很容易产生错误。Oce自动变量到达其作用域的末尾时,访问将不再安全。更糟糕的是,这通常不会立即生成一个可见的错误,这会让您挠头,稍后在错误的位置查找错误。关于字符串指针到字符数组的警告:如果您错误地存储了一个自动(也称为局部)变量,则很容易产生错误。Oce自动变量到达其作用域的末尾时,访问将不再安全。更糟糕的是,这通常不会立即生成一个可见的错误,让您挠头,稍后在错误的位置查找错误。