在用构造函数实现数据成员初始化的时候遇到了“表达式必须时刻是可修改的左值”的问题。
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
Student(int n , string nam , char s)
{
num = n; name = nam; sex = s;
cout << "Constructor call." << endl;
}
~Student()
{
cout << "Des call." << endl;}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num; string name[10]; char sex;
};
int main() {
Student stu1(111, 'wang', 'm');Student stu2(112, 'ze', 'f');
stu1.display(); stu2.display();
return 0;
}
看了一下错误在于name这个数据成员。
解决方法:
(1)尝试全部替换为char,无果。
等待新的解决办法。