C++程序哪出错了?,。得不到信息存放在student.txt里面。><。求助

2020-10-21 旅游 63阅读
注:修正有注释行的地方,应该就没什么问题了
#include
#include
using namespace std;
class person
{
public:
person(char name[20],char sex,int age) //<--构造函数的参数命名和成员变量名一样,你能区分是谁赋值给谁吗?改为person( char _name[20], char _sex, int _age )
{
name[20]=name[20]; // <-- memcpy( name, _name, 20 );
sex=sex; //<--sex = _sex;
age=age; // age = _age;
}
protected:
char name[20];
char sex;
int age;
};
class student:public person
{
public:
student(char name[20],char sex,int age,int num):person(name,sex,age)
// student( char _name[20], char _sex, int _age, int _num ): person( _name, _sex, _age )
{
num=num; // <-- num = _num;
}
protected:
int num;
};
int main()
{
student stu[10]=
{
student("angel one",'m',20,1),
student("angel two",'m',20,2),
student("angel three",'m',20,3),
student("angel four",'m',20,4),
student("angel five",'m',20,5),
student("angel six",'m',20,6),
student("angel seven",'m',20,7),
student("angel eight",'m',20,8),
student("angel nine",'m',20,9),
student("angel ten",'m',20,10)
};
ofstream outfile("student.txt",ios::binary);
if(!outfile)
{
cerr<<"open error!"< abort();
}
int i;
for(i=0;i<10;i++)
outfile.write((char*)&stu[i],sizeof(stu[i]));
outfile.close();
return 0;
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com