C++构造函数问题,谭浩强第三版9.5例题:
#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 called." << endl;
}
~Student()
{
cout << "Constructor called." << endl;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl << endl;
}
private:
int num;
char name;
char sex;
};
int main()
{
Student stu1(10086,"Zhang_wu_ji",'M');
stu1.display();
Student stu2(10000,"Zhao_ming",'F');
stu2.display();
return 0;
}最后提示“error C2440: “=”: 无法从“std::string”转换为“char ”,这需要怎么修改才行{:9_220:}
” name=nam.c_str()
好像是这样
页:
[1]