【发布时间】:2019-03-27 20:08:02
【问题描述】:
请帮助错误:没有构造函数实例与参数列表匹配。 也请帮忙解释一下“strcpy(this->name, name);”
class Student {
char name[50];
char surname[50];
int age;
public:
Student(char name[], char surname[], int age) {
strcpy(this->name, name); // please explain this line what does it means?
strcpy(this->surname, surname);
this->age = age;
}
void Show() {
cout << "Name: " << this->name << endl;
cout << "Surname: " << this->surname << endl;
cout << "Age: " << this->age;
}
};
int main() {
Student A("Ivan", "Sidoroff", 25);
A.Show();
system("pause");
return 0;
}
【问题讨论】:
-
1) 您显示的代码compiles just fine。 2)
strcpy的作用在strcpy的文档中有说明。 -
@AlgirdasPreidžius 您需要打开警告,然后 ISO C++11 不允许从字符串文字转换为 'char *'
-
@NathanOliver 我认为在 C++11 及更高版本上这是一个错误,而不是警告,并且由于我使用 C++14 编译,我认为如果有任何此类错误,它不会编译。 :/ 诅咒你 g++ 和你的非标准扩展!
-
@AlgirdasPreidžius 如果有帮助,我在编译时总是使用
-Wall -pedantic。我也使用 Wandbox 或 coliru,因为您实际上可以指定所有选项并查看所有警告。 -
-Wall -Wextra -Werror -pedanticftw
标签: c++ constructor this-pointer