我的ostream friend功能正常工作,但我的ofstream和ifstream正常工作,因为给出的错误表明不能通过ofstream或ifstream函数访问不同的私有成员。请帮忙。
class Module
{
public:
Module() = default;
explicit Module(const string& purpose, const string& id)
{
this->purpose = purpose;
this->id = id;
}
explicit Module(const string& purpose) { this->purpose = purpose; }
~Module() { }
virtual void setCrew( ) = 0;
virtual void display( ) = 0;
void addCrew(Crew c) { crew_list.push_back(c); }
void setPowerReq(double power) { this->power = power; }
void setMaxCrew(int max_crew) { this->max_crew = max_crew; }
string getPurpose( ) { return purpose; }
string getId( ) { return id; }
int getNumberOfCrew( ) { return number_of_crew; }
int getMaxCrew( ) { return max_crew; }
double getPower( ) { return power; }
friend ostream& operator << ( ostream& os, const Module& m );
friend ofstream& operator << ( ofstream os, const Module& m );
friend ifstream& operator >> ( ifstream is, Module& m );
private:
static string purpose;
string id = "unknown";
int number_of_crew = 0;
int max_crew = 0;
double power = 0;
vector<Crew> crew_list;
};
#include "module.h"
string Module::purpose = "Module";
ofstream& operator << ( ofstream& os, const Module& m )
{
os << m.purpose << endl
<< m.id << endl
<< m.number_of_crew << endl
<< m.max_crew << endl
<< m.power << endl;
for ( auto i : m.crew_list )
{
os << i << endl;
}
return os;
}
ifstream& operator >> ( ifstream& is, Module& m )
{
getline( is, m.purpose );
return is;
}
ostream& operator << ( ostream& os, const Module& m )
{
os << m.purpose << endl
<< m.id << endl
<< m.number_of_crew << endl
<< m.max_crew << endl
<< m.power << endl;
for ( auto i : m.crew_list )
{
os << i << endl;
}
return os;
}