I have these classes for a start:
Name ( string fName, string lName)
Student (Name name, int age)
Both classes classes have the < operator overloaded.
Now I have added a third class:
StudentContainer
StudentContainer creates a vector ”studentcontainer” with Student objects.
I need to sort the Student objects in the StudentContainer vector. I need to do it 2 ways:
SortByName
SortByAge
- - -
The < overload in the Student class:
1 2
|
//In Student.h
bool operator<(const Student &student) const;
|
1 2 3 4 5
|
//In Student.cpp
bool::Student::operator<(const Student &student) const
{
return this->name < student.name;
}
|
The SortByName function in StudentContainer:
1 2
|
//In StudentContainer.h
void sortByName();
|
1 2 3 4 5
|
//In StudentContainer.cpp
void StudentContainer::sortByName()
{
sort(studentcontainer.begin(), studentcontainer.end());
}
|
- - -
This results in the studentcontainer objects being sorted by name using the overload of the Student < operator. So far so good.
The second version of sorting is supposed to be void and have no parameters:
void sortByAge();
I have not been able to figure out how to make this one work. The overloaded < in the Student class only does sorting by name. And since sortByAge() is required to be void and lack parameters, I can’t see how I could use a special function for sorting by age as a third parameter when calling sort() as I’ve seen suggested here for instance:
http://www.cplusplus.com/articles/NhA0RXSz/
That way to go seems to require both return type and parameters, rather than a void function with no parameters…
Any suggestions (preferably with code please)?