I have a problem using the STL containers in c++
function 1;
void addStudent(const Student &s){
set<GradeColumn>::iterator itr;
for(itr = gradeColumns.begin(); itr != gradeColumns.end(); itr++){
itr->addStudent(s, DID_NOT_COMPLETE);
}
}
function 2:
void addStudent(const Student &s, int grade) const {
column.insert(pair<Student, int>(s, grade));
}
Okay, so in function 1 I use addStudent, but the compiler gives me an error unless I declare addStudent as a const function (the error is “The object has type qualifiers that are not compatible with the member function”).
But if I do declare addStudent as a const function, then column.insert(…) gives me thee next error: “No instance of overloaded function matches the argument list and object 9the object has type qualifiers that prevent a match)”.
Is there any way to fix that? Am I missing something?
Thanks!
The problem here is that the class you are trying to modify is also the key in the set. And this is required to be immutable. i.e. it is not correct behaviour to change the key once the item is in the set.
You need to consider a different structure, I would advice that you extract the key element out into a separate key structure and use a map to map the key to the mutable data content of your structure.