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 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.
Your loop is actually equivalent to the following `for` loop:

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

```

Notice how that last part of the `for` loop is empty? That means you you never increment or otherwise modify the variable `it` in the `for` statement, and *neither does the loop body!* That means `it` will never change and you have an infinite loop.

The simple and obvious way to fix this is to increment `it` in the loop. Back to your original loop, with the fix added:

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

    ++it;  // Make iterator "point" to the next node
}

```

***However*** this fix is flawed in another way. This is because when you remove a node, you will skip over the node after the node you remove, so you miss a node. The naive solution is to only increment `it` if you don't remove a node:

```
while (it != studentList.end()){
    if(*it== studentName){
        studentList.erase(it);
    } else {
        ++it;  // Make iterator "point" to the next node
    }
}

```

This solution is flawed in that you will have *undefined behavior* if you remove a node. This is because then `it` will not be updated, and the next iteration of the loop you will dereference an iterator to a node that no longer exist. The solution to *this* problem is to know what [the `erase` function *returns*](http://en.cppreference.com/w/cpp/container/list/erase), namely an iterator to the following node. That means a working solution would look like

```
while (it != studentList.end()){
    if(*it== studentName){
        it = studentList.erase(it);  // Make iterator "point" to node after removed node
    } else {
        ++it;  // Make iterator "point" to the next node
    }
}

```