I'm trying to remove objects from a vector while looping over it. I tried to implement "Erase-remove idiom" but still can't get it to work, I've made several re-writes to try make it work but still end up getting error.
Here's the function meant to remove objects from the vector (parameter 1)
void ta_bort_person(std::vector <Person>& telReg, std::string fnamn){
int j = 0;
int index;
bool continueLooping = true;
Person tempPerson;
while (continueLooping) {
for (int i = 0; i < telReg.size(); i++) {
if (telReg[i].fnamn == fnamn) {
std::string enamn = telReg[i].enamn;
std::string nummer = telReg[i].nummer;
std::cout << fnamn << " " << enamn << " tas nu bort." << std::endl;
tempPerson = telReg[i];
break;
//tempList.push_back(person);
}
continueLooping = false;
}
if (continueLooping) {
telReg.erase(std::remove(telReg.begin(), telReg.end(), tempPerson), telReg.end());//this is where I get error
}
}
} Person struct:
struct Person
{
std::string fnamn;
std::string enamn;
std::string nummer;
};
Message from compiler:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xmemory(2018,28): error C2676: binary '==': 'Person' does not define this operator or a conversion to a type acceptable to the predefined operator
1>C:\Users\chris\source\repos\Labb1-CPROG\Labb1-CPROG\Source.cpp(99): message : see reference to function template instantiation '_FwdIt std::remove<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,Person>(_FwdIt,const _FwdIt,const _Ty &)' being compiled
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Person>>>,
1> _Ty=Person
1> ]
1>Done building project "Labb1-CPROG.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========