Reading objects with string fields from file

How to read file like this:

John; Michaels; USA;
Nick; Davidson; France;

into vector<Person> where class Person contains fields name, surname, country?

I tried to overload operator >> for ifstream but it shows error that string cannot be at the right side of this operator? Is there any other way like using stringstream or something else?
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct Person
{
  string name;
  string surname;
  string country;
};

istream& operator >> (istream& is, Person p)
{
  getline(is, p.name, ';');
  getline(is, p.surname, ';');
  getline(is, p.country);

  return is;
}

ifstream src("Your file name");
if (!src)
{
  // handle error
}
Person p;
vector<Person> persons;
while (src >> p)
  persons.push_back(p);
Worked! Thank you
Shouldn't line 8 pass the Person by reference?
 
istream& operator >> (istream& is, Person& p)
Shouldn't line 8 pass the Person by reference?

Sure, silly mistake.
Topic archived. No new replies allowed.