网上的指导方法是:
https://blog.csdn.net/weixin_37959984/article/details/82782984
我的方式是把结构体中定义的 char * 前面加上 "const"
示例及改正代码:
#include <iostream>
using namespace std;
class Student {
public:
//char *name; //错误!!!
const char *name; //需要在这里写上 const
int age;
float score;
void say() {
cout << name << "的年龄是" << age << ",成绩是" << score << endl;
}
};
int main() {
Student *pStu = new Student;
pStu->name = "小明";
pStu->age = 15;
pStu->score = 92.5f;
pStu->say();
delete pStu; //删除对象
getchar();
return 0;
}