class user
{
private:
std::string first_name;
std::string middle_name;
std::string last_name;
int ID;
static int next_id;
public:
static int next_user_id()
{
next_id++;
return next_id;
}
group User_Group;
void set_ID(int c)
{
ID=c;
}
int get_ID()
{
return ID;
}
void set_first_name(std::string c)
{
first_name=c;
}
string get_first_name()
{
return first_name;
}
void set_middle_name(std::string c)
{
middle_name=c;
}
string get_middle_name()
{
return middle_name;
}
void set_last_name(std::string c)
{
last_name=c;
}
string get_last_name()
{
return last_name;
}
user()
{
ID = user::next_id++;
}
friend istream operator>>(istream is, user User);
friend ostream operator<<(ostream os, user User);
};
int user::next_id;
istream operator>>(istream is, user User)
{
is >> User.get_first_name();
is >> User.get_middle_name();
is >> User.get_last_name();
is >> User.get_ID();
is >> User.User_Group.get_name();
for(int j=0;j<=4;j++)
{
is >> User.User_Group.Week_Food[j].get_breakfsat();
is >> User.User_Group.Week_Food[j].get_lunch();
is >> User.User_Group.Week_Food[j].get_dinner();
}
for (int j=0;j<=30;j++)
{
is >> User.User_Group.Month_Food[j].get_breakfsat();
is >> User.User_Group.Month_Food[j].get_lunch();
is >> User.User_Group.Month_Food[j].get_dinner();
}
}
ostream operator<<(ostream os, user User)
{
os<<User.get_first_name()<<" "<< User.get_middle_name() <<" "<<User.get_last_name()<<" ";
for(int j=0;j<=4;j++)
{
os<<User.User_Group.Week_Food[j].get_breakfsat()<<" "<<User.User_Group.Week_Food[j].get_lunch()<<" "<< User.User_Group.Week_Food[j].get_dinner()<<" ";
}
for (int j=0;j<=30;j++)
{
os<< User.User_Group.Month_Food[j].get_breakfsat()<<" "<<User.User_Group.Month_Food[j].get_lunch()<<" "<<User.User_Group.Month_Food[j].get_dinner();
}
}
我是 C++ 新手,也许这是一个愚蠢的问题,但我需要将对象数组写入文件并通过重载 << 和 >> 运算符从那里读取它。当我尝试将它用于 int 和 bool 类的成员时,我得到了错误。它似乎适用于字符串类型,所以我不知道如何继续。