Hello i'm curently coding a fonction that erase element from a vector of pointer(to class object), but i cant quite make it work.
I get this error
error: no matching function for call to ‘std::vector<biblio::Reference*>::erase(biblio::Reference*&)’
std::vector<Reference*> m_vReferences; //Reference is a class
for ( auto iter : m_vReferences) //Loop for on every pointer to a class object
{
if (iter->reqId () == p_id) //Check if the id of the class object is the id we want
{
m_vReferences.erase (iter); //Erase the pointer in the vector of pointer
}
else
{
throw EmptyReferenceException (iter->reqFormatedReference ()); //An exception
}
}
CodePudding user response:
Don't use auto-range loops when you want to delete the element from the container. I would use std::remove_if as it is available in standard library.
m_vReferences.erase(std::remove_if(m_vReferences.begin(),m_vReferences.end(),[p_id](Reference* x){
return x->reqId() == p_id;
}),m_vReferences.end());
or you may loop through vector find at which index is the element you want to delete and use erase function from vector.