Question: Im having trouble with STl. Im trying to iterate through an STL list of student objects. Im trying to delete objects when I find a matching comparison. However I receive an error when doing the comparison. This is what I have done so far:

```
 string studentName;
                cout<<"Enter name of student to remove";
                cin>>studentName;

                list<Student>::iterator it = studentList.begin();
                while (it != studentList.end()){
                    if(*it== studentName){
                        studentList.erase(it);
                    }
                }

```

I get the error "Invalid operands to binary expression ('value\_type' (aka 'Student') and 'string' (aka 'basic\_string, allocator >'))"
I'm not quite sure how to solve it.
Thank you, any advice is appreciated!

Answer: 
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;
    }
}

```
You are comparing an instance of `Student` with an `std::string` which I'm assuming doesn't have a defined `operator==` overloaded function. You can either define this operator or compare `studentName` with the member string variable in `Student` which stores the name of the student. You might consider looking at `std::remove_if` in the algorithms library which you can use to filter out any students which don't have that name.