Oops. Should have copied and pasted rather than typed it. Here's the broken code as it stands (I made the array smaller for brevity):
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
int main()
{
class student
{
public:
std::string name;
int grade;
bool operator<(const student& rhs) const
{
return grade < rhs.grade;
}
};
student student_array[4];
student_array[0].name = "Joe";
student_array[0].grade = 85;
student_array[1].name = "Sara";
student_array[1].grade = 72;
student_array[2].name = "Mike";
student_array[2].grade = 98;
student_array[3].name = "Bob";
student_array[3].grade = 65;
int i;
std::cout << "UNSORTED" << std::endl;
for(i = 0; i < 4; ++i)
{
std::cout << student_array[i].name << ":" << student_array[i].grade << std::endl;
}
std::sort(student_array, student_array + 4);
std::cout << std::endl << "SORTED" << std::endl;
for(i = 0; i < 4; ++i)
{
std::cout << student_array[i].name << ":" << student_array[i].grade << std::endl;
}
return 0;
}
The compiler issues:
error: no matching function for call to ‘sort(main()::student [4], main()::student*)’
If I put the class definition outside main(), if works with no problem, but I don't want to do this if I can avoid it.