今日bug:error: invalid array assignment

错误代码:

struct STUD
{
    int ID;//学号
    char name[20];
    float score;
}stud;
STUD SS[10];
student.open("student.dat",ios::in|ios::binary);
    if(!student)
    {
        cout<<"打开文件失败!"<<endl;
        return 0;
    }
    int j=0;
    student.read((char *)&stud,sizeof(stud));
    while(!student.eof()){
        SS[j].name=stud.name;//报错!
        SS[j].ID=stud.ID;
        SS[j].score=stud.score;
        j++;
        student.read((char *)&stud,sizeof(stud));
    }
    student.close();

这段代码的目的是从文件.dat中读取数据,存储到结构体数组对象SS[j]中,但是对SS[j].name赋值是报错,改成了strcpy(SS[j].name,stud.name)即可,这里若用等号需要重载运算符=。注意添加头文件#include<cstring>

猜你喜欢

转载自www.cnblogs.com/muyuying/p/10714109.html