オブジェクトの配列をプライベート変数として持つクラスの読み込み関数と出力関数を定義する必要があります。テキストファイルからオブジェクトを読み込んで画面に出力する必要があります。これを行うには、< <と>>演算子をオーバーロードする必要があります。配列に格納された情報を読み取り、印刷するためにループを使用する必要があることは理解していますが、これをどのように達成するのかはわかりません。私の講師は、基本的に関数プロトタイプであるスケルトンコードと私が守るべき主な機能を与えてくれました。私は、これを使ってこの厳密なシナリオを実行したが、クラスのプライベート変数が私を引き上げているので、これが公共の構造体とどのように機能するのか理解しています。I/Oのオーバーロードとテキストファイルからの読み込み
class EmployeeList {
public:
//Constructors
EmployeeList();
EmployeeList(istream&);
//Accessors
bool isEmpty() const;
bool isFull() const;
int size() const; //Number of employees in list
Employee item(int i) const; //i'th employee
//Mutators
void setItem(int i,const Employee& e);
//I/O functions, sets the i'th emplyee to e
void read(istream&);
void print(ostream&) const;
private:
enum {MAXSIZE = 100};
Employee list[MAXSIZE];
int count; //Number of employees in the current list
};
EmployeeList::EmployeeList() {
count = 0;
}
EmployeeList::EmployeeList(istream& in) {
//list[MAXSIZE] = in;
}
bool EmployeeList::isEmpty() const {
return (count == 0);
}
bool EmployeeList::isFull() const {
return (count == MAXSIZE);
}
int EmployeeList::size() const {
return count;
}
Employee EmployeeList::item(int i) const {
}
void EmployeeList::setItem(int i, const Employee& e) {
}
void EmployeeList::read(istream& in) {
Employee tempList;
while (in >> tempList) {
}
}
void EmployeeList::print(ostream& out) const {
for (int i=0; i < size(); i++) {
}
cout << out;
}
上記の部分はクラスEmployeeListですが、以下の部分はオーバーロード関数です。コメントされた部分は、私が働いていたかもしれないと思ったアイデアです。
istream& operator>>(istream& in, EmployeeList& l) {
l.read(in);
return in;
}
ostream& operator<<(ostream& out, const EmployeeList& l) {
l.print(out);
return out;
}
以下は、私たちに与えられた主な機能です。
int main() {
authorInfo();
ifstream infile("a1in.txt");
if(!infile) {
cout << "file 'alin.txt' not found.";
return EXIT_FAILURE;
}
EmployeeList theList(infile);
cout << endl;
cout << theList.size() << " employees read:\n" << theList << endl;
process(theList);
return EXIT_SUCCESS;
}
誰かが私を正しい方向に向けることができますか?あなたがもっとコードを必要とするかどうかを教えてください。ありがとう!
EDIT: 従業員の読み取り及び印刷機能:過負荷
void Employee::read(istream& in) {
in >> name >> id >> salary;
}
void Employee::print(ostream& out) const {
out << getName() <<" "<< getID() <<" "<< getSalary() << endl;
}
従業:
istream& operator>>(istream& in, Employee& e) {
e.read(in);
return in;
}
ostream& operator<<(ostream& out, const Employee& e) {
e.print(out);
return out;
}
EDIT 2:更新読み出し()関数。 whileとの行はエラーの場所です。
void EmployeeList::read(istream& in) {
Employee inEmployee;
while (in >> inEmployee && count < MAXSIZE) {
list[count] = inEmployee;
count++;
}
}
EDIT 3:これまで私が持っていたprint()関数があります。実際には印刷されますが、ファイルからの情報ではなく、デフォルトのコンストラクタ情報が得られます。これは読み取りまたは印刷機能の問題ですか?私は読書機能をまだ考えている。
void EmployeeList::print(ostream& out) const {
cout << endl;
for (int i=0; i < count; i++) {
out << list[count];
}
}
プライベート変数があなたを引き上げるのはなぜですか?パブリックセクションでは、必要なすべてのものが一見わかりやすいように見えます。 – BugFinder
名前、ID、給与の空白はEmployee :: readを破ります。 –