You are trying to compare Student with string. Such a comparison is not defined by default so either you have to define a proper operator(s) yourself or write something like (*it).getName() == studentName
where getName is a member function of Student which returns the name of the student.
In addition, your for loop isn't correct. It should be something like this:
for(auto it = studentList.begin(); it != studentList.end();) {
if((*it).getName() == studentName) {
it = studentList.erase(it);
} else {
++it;
}
}
EDIT: If you decide to overload comparison operators then here is a tip on how to do it:
bool operator==(const Student& student, const std::string& name) {
return student.getName() == name;
}
bool operator==(const std::string& name, const Student& student) {
return student == name;
}
bool operator!=(const Student& student, const std::string& name) {
return !(student == name);
}
bool operator!=(const std::string& name, const Student& student) {
return !(student == name);
}
For the purpose of this question the first of the above four overloads would be enough but usually it's better to define a few versions to avoid any surprises in the future. Also, if the Student class doesn't have any member function like getName (Having such a function is strongly advised unless Student is a simple struct with all data members public.) then you have to change the first overload (The rest of them refer to the first one so they will automatically adjust to the changes.) like this:
bool operator==(const Student& student, const std::string& name) {
return student.name == name;
}
Furthermore, if the name of the Student is private or protected and there is no way to access it from public context then you also have to add a friend declaration to your Student definition:
class Student {
public:
// Public interface...
private:
std::string name;
friend bool operator==(const Student& student, const std::string& name);
};
The position of the friend declaration doesn't matter as long as it's inside the class' definition. Again, you only need to make the first of the overloads privileged because the rest of them just call the first one.
Now the loop can be changed:
for(auto it = studentList.begin(); it != studentList.end();) {
if(*it == studentName) {
it = studentList.erase(it);
} else {
++it;
}
}