Person perobj[];//how to find the number of objects?, it should be the number of line in the file
int i=0;
while(!data.eof()){
data>>perobj[i].setname()>>perobj[i].setage()>>perobj[i].setgender();
i++;
//here is one of my problems! don't know how to go to the next line
}
class Person
{
// some members ...
friend std::istream &operator >> ( std::istream &, Person & ); // so that >> can access private members
};
operator >> implementation:
1 2 3 4 5
std::istream &operator >> ( std::istream &is, Person &pers)
{
is >> pers.name >> pers.age >> pers.gender; // use directly member variables, not setter functions
return is;
}
While reading -let's say you have a list-:
1 2 3
Person temp; // declare temporary Person object
data >> temp; // read it from the file
person_list.push_back ( temp ); // add it to the list - this way data could contain any number of people