sort function

I have no idea why this sort function is not working. Help would be appreciated
Im trying to get the list of people sorted by their birth year. But I cant figure out how to work this function.


1
2
3
4
5
6
7
8
9
//declariation
bool sort_years(const Person& year1,const Person& year2);

//definition
bool Person :: sort_years(const Person& year1, const Person& year2)
{ return year1.birth_year < year2.birth_year; }

// calling function (ive tried sevral variations of this)
  sort(persident.begin, persident.end, sort_years ()); // error is this line "sort_years was not devclaired in scope" 
Last edited on
Your declaration of sort_years doesn't match the defintion. Remove Person :: from the definition.

Line nine should look like this:
sort(persident.begin(), persident.end(), sort_years);
Thank you! but now Im having trouble displaying it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//declariation
bool sort_years(const Person& year1,const Person& year2);

//definition
bool sort_years(const Person& year1, const Person& year2)
{ return year1.birth_year < year2.birth_year; }

// calling function (ive tried sevral variations of this)
 sort(persident.begin(), persident.end(), sort_years ); not devclaired in scope" 

 I framed this after the reference page this site has on the sort function. 
I think the problem  I don't understand the "::iterator" which I figure is the problem.  

for (vector<Person>::iterator it=persident.begin(); it!=persident.end(); ++it)
    cout << ' ' << *it; // upon further testing ive found that the problem is with the pointer although im still not sure why
    cout << '\n'; 
Last edited on
You're trying to output a Person object, right? cout << knows all about the basic types. It knows what to do with an int. It knows what to do with a string. It knows what to do with a double. It does not know what to do with a new kind of object you created, a Person, unless you wrote the code for that.

Did you write code for that? If you didn't write code for that, you either have to write code for that, or change what you're trying to output. For example, birth_year is a standard type, I guess, so cout << knows what to do with it:

cout << (*it).birth_year;
Last edited on
Topic archived. No new replies allowed.