我的方式是把结构体中定义的 char * 换成string
#include <iostream> #include<string> using namespace std; class Student { public: void setname(string name); void setage(int age); void setscore(float score); void show(); private: string name; int age; float score; }; void Student::setname(string name) { this->name = name; } void Student::setage(int age) { this->age = age; } void Student::setscore(float score) { this->score = score; } void Student::show() { cout << this->name << "的年龄是" << this->age << ",成绩是" << this->score << endl; } int main() { Student *pstu = new Student; pstu->setname("小明"); pstu->setage(18); pstu->setscore(100.0); pstu->show(); return 0; }