'C++ No matching member function for call to 'erase'?
Alright, so I am building a game that requires that I am able to add and remove a *Fighter object.
I first declare member variable fighter here
std::vector<Fighter*> fighter;
I then implement Add and Remove like this:
void Game::AddFighter(Fighter* f) {
fighter.push_back(f);
}
void Game::RemoveFighter(Fighter* f) {
if ( std::find(fighter.begin(), fighter.end(), f) != fighter.end() )
{
fighter.erase(f); //Error here
}
}
On the line indicated above, Im getting the error
No matching member function for call to 'erase'
I understand that the member calling erase must be an iterator, however I don't know how to do that in the context of what I am trying to achieve. I know that the above code works for detecting a Fighter* if the Fighter* has been added - how do I use the erase() function?
Solution 1:[1]
You need to pass an iterator to erase:
if ( auto it = std::find(fighter.begin(), fighter.end(), f); it != fighter.end() )
{
fighter.erase(it); //No error here. If you use C++17.
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | littleadv |
