I have a vector of my user-defined data type city
. I'm trying to remove an element from this vector by its ID; eventually all cities will be removed from this list in a while(!cityList.empty())
loop. I plan to use the erase-remove idiom to accomplish this.
However, I'm getting a very gnarly error message when I make my code after calling remove()
. Passing in the city
object as the third parameter to remove()
results in this error, as does passing in the (int) ID of the city
. This error doesn't occur with erase()
, but does with remove()
, and also find()
if I try to use that. Here is the code in question:
vector<city> cityList;
cityList.push_back(city(1));
cityList.push_back(city(2));
cityList.push_back(city(3));
city cityToRemove = cityList[0];
int idOfCityToRemove = cityList[0].getID();
remove(cityList.begin(), cityList.end(), cityToRemove);
//remove(cityList.begin(), cityList.end(), idOfCityToRemove);
Here is an updated simple, minimal demo of my problem.
The error message includes "template argument deduction/substitution failed" and "‘city’ is not derived from ‘const _gnu_cxx::__normal_iterator<_IteratorL, _Container>’ " messages, and I haven't been able to find anything online relating to my problem with the above errors.
Edit: I've modified my code such that I now have:
int main(int argc, char** argv) {
vector<city> cityList;
cityList.push_back(city(1));
cityList.push_back(city(2));
cityList.push_back(city(3));
city cityToRemove = cityList[0];
int idOfCityToRemove = cityList[0].getID();
int i;
for (i = 0; i < cityList.size(); i++) {
cityList.erase(remove_if(cityList.begin(), cityList.end(), cityList[i] == cityToRemove), cityList.end());
}
//remove(cityList.begin(), cityList.end(), cityToRemove);
//remove(cityList.begin(), cityList.end(), idOfCityToRemove);
return 0;
}
bool operator ==(const city &a, const city &b)
{
return (a.id == b.id);
}
and the error I receive when attempting to compile is:
In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from main.cpp:2:
/usr/include/c++/5/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<city*, std::vector<city> >; _Predicate = bool]’:
/usr/include/c++/5/bits/stl_algo.h:866:20: required from _ForwardIterator std::__remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<city*, std::vector<city> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<bool>]’
/usr/include/c++/5/bits/stl_algo.h:936:30: required from ‘_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator<city*, std::vector<city> >; _Predicate = bool]’
main.cpp:30:90: required from here
/usr/include/c++/5/bits/predefined_ops.h:234:30: error: expression cannot be used as a function
{ return bool(_M_pred(*__it)); }
^
This is closer, but I'm not sure what is required. The line main.cpp:30:90
points to the cityList[i] == cityToRemove
part of my cityList.erase()
function, however, so I know that the issue is within my comparison expression.
My demo is also updated.