【问题标题】:constructor error for array of structure, error msg: no instance of constructor matches the argument list结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
【发布时间】:2018-09-08 03:19:28
【问题描述】:
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::Student 与参数列表匹配,我想不出问题。 这是构造函数:

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

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

【问题讨论】:

  • 关于指向字符数组的字符串指针的警告:如果您错误地存储了自动(AKA 本地)变量,则很容易拥有dangling pointer。一旦 Automatic ariable 到达其范围的尽头,它就不再安全访问。更糟糕的是,这通常不会立即产生明显的错误,让您摸不着头脑,稍后在错误的位置寻找错误。

标签: c++ constructor compiler-errors


【解决方案1】:

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

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

您还需要确保在Student 中将name 声明为const char*

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2016-03-05
    • 2020-06-09
    相关资源
    最近更新 更多