求解
包含构造函数和析构函数的C++程序
编写程序:
#include<string>
#include<iostream>
using namespace std;
class Student
{
public:
Student(int n,string nam,char s)
{
num=n;
name=nam;
sex=s;
cout<<"Constructor called."<<num<<endl<<endl;
}
~Student()
{
cout<<"Destructor called."<<num<<endl<<endl;
}
void display()
{
cout<<"num:"<<num<<endl<<endl;
cout<<"name:"<<name<<endl<<endl;
cout<<"sex:"<<sex<<endl<<endl;
}
private:
int num;
char name[10];
char sex;
};
int main()
{
Student stud1(10010,"Wang_li",'f');
stud1.display();
Student stud2(10011,"Xue_hua",'m');
stud2.display();
return 0;
}
它的报错是:D:\main\练习0.cpp(10) : error C2440: ‘=’ : cannot convert from ‘class std::basic_string<char,struct std::char_traits,class std::allocator >’ to ‘char [10]’
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
(这是C++程序设计第三版(谭浩强编著的)中的例题,但是运行不了)
有没有大佬指点下为啥会这样,怎样修改?