今日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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM