以下のコードを使用して、Student
配列を取り、gpa
コンポーネントに基づいて並べ替えるために、並べ替え機能を使用したいと思います。私はStudent
配列のパラメータと配列のサイズを使用する必要があります。私のint main
関数の底に向かって見ると、配列ソートのためにメンバーソートを呼び出そうとしていますが、無駄はありません。a
私が得るエラーは:クラス配列でメンバー関数を使用しようとしています
メンバー参照ベースタイプ
Student [200]
は構造体または共用体ではありません。
はどのように配列a
を取ると、それは私が使用する必要があるパラメータを指定した上でメンバーSort
を使用するように言って私のコードを書いてください。前もって感謝します。これがあまりにも大きければ、さらに詳しく指定しようとします。
class Student
{
private:
string ID, fname, lname, level;
double gpa;
public:
Student();
Student(string id, string first, string last, double Gpa, string grade);
string getID() const;
string getfirst() const;
string getlast() const;
string getlevel() const;
double getGPA() const;
void setID(string id);
void setfirst(string f1);
void setlast(string l1);
void setlevel(string lev);
void setGPA(double GPA1);
friend void Sort(Student studentlist[], int size);
friend ostream& operator <<(ostream& ost, Student S1);
};
int main()
{
ifstream ins;
ofstream outs;
ins.open("input.dat");
outs.open("output.dat");
if(ins.fail())
{
cout << "File not found.";
exit(1);
}
if(outs.fail())
{
cout << "Output file not opened.";
exit(1);
}
Student a[200];
int x = 0;
while(!ins.eof())
{
string id, fnam, lnam, levl;
double point;
ins >> id >> fnam >> lnam >> point >> levl;
a[x].setID(id);
a[x].setfirst(fnam);
a[x].setlast(lnam);
a[x].setGPA(point);
a[x].setlevel(levl);
if(a[x].getID() == "")
{
break;
}
x += 1;
}
if(x == 0)
{
cout << "File is empty" << endl;
return 0;
}
x = x +1;
a.Sort(a, x);
int t=0;
while(t<x)
{
outs << a[t];
t += 1;
}
outs.close();
ins.close();
return 0;
}
フレンド関数はメンバーではありません。 – aschepler
に関して:while(!ins.eof()) '、[なぜループ状態の中のiostream :: eofが間違っていると思われますか?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-ループ内のループ条件が間違っている) – user4581301
残念ながらトピック: 'exit(1);'は 'main'では不要です。' return 1; ' – user4581301