I'm trying to sort a vector of Student
objects by an attribute:
class Student
{
private:
std::string nume;
int an;
std::list<Curs> cursuri;
...
public:
Student();
Student(std::string nume, int an);
virtual ~Student();
...
};
with these sorting method comparatator:
bool Student::sortByMedie(const Student& a, const Student& b)
{
return a.medie < b.medie;
}
void sortStudenti(std::vector<Student> studenti) {
std::sort(studenti.begin(), studenti.end(), Student::sortByMedie);
for (auto student : studenti) {
student.afisare();
}
}
But I am encounter a problem with a stack overflow exception when the sort method is called:
The thread 0x4f6c has exited with code 0 (0x0). Exception thrown at 0x776CBA3E (ntdll.dll) in LAB3.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x01002FF0). Unhandled exception at 0x776CBA3E (ntdll.dll) in LAB3.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x01002FF0).
I'm assuming the problem is somewhere in a reallocation of vector size in memory. If I browse through the stack trace beyond the memory allocation functions, the last function of my own code (i.e. not standard library) is a Curs
copy constructor called by a swap between two Cusr
elements, that is invoked by Curs::operator=
This is the creation of vector:
std::vector<Student> studenti;
auto student1 = Student("gigel marian", 3);
student1.addCursuri(generateCoursList());
auto student2 = Student("gigel marian2", 3);
student2.addCursuri(generateCoursList());
auto student3 = Student("gigel marian3", 3);
student3.addCursuri(generateCoursList());
auto student4 = Student("gigel marian4", 3);
student4.addCursuri(generateCoursList());
auto student5 = Student("gigel marian5", 3);
student5.addCursuri(generateCoursList());
studenti.push_back(student1);
studenti.push_back(student2);
studenti.push_back(student3);
studenti.push_back(student4);
studenti.push_back(student5);
In first place I tried with this method:
void sortStudenti(std::vector<Student> studenti) {
struct studentCompare
{
bool operator()(Student const& a, Student const& b)
{
return a.getMedie() > b.getMedie();
}
};
std::sort(studenti.begin(), studenti.end(), studentCompare());
for (auto student : studenti) {
student.afisare();
}
}
but I got some const
access errors, so I tried in another way.
Edit: additional code
The full code is available on github