Reputation: 534
I've looked at similar problems but I simply can not see what is going wrong. I've tried ::const_iterator as well. But gcc will not compile either way.
it's about : i = allFunctions.erase(i);
void removeEventListener(const std::string &type, function listener){
if(!hasEventListener(type))
return;
std::map<int, std::list<function> > &allFunctions = eventHandlerList[type];
std::map<int, std::list<function > >::iterator i;
for(i=allFunctions.begin(); i!=allFunctions.end(); ++i)
{
i->second.remove(listener);
if(i->second.empty())
{
i = allFunctions.erase(i);
}
}
if(allFunctions.empty())
eventHandlerList.erase(type);
}
error with const_iterator:
Error: passing const std::list<void (*)(const st::event::Event&)> as
this argument of void std::list<_Tp, _Alloc>::remove(const
value_type&) [with _Tp = void (*)(const st::event::Event&), _Alloc =
std::allocator<void (*)(const st::event::Event&)>, std::list<_Tp,
_Alloc>::value_type = void (*)(const st::event::Event&)] discards qualifiers [-fpermissive]
Error: no matching function for call to std::map<int, std::list<void
(*)(const st::event::Event&)> >::erase(std::map<int, std::list<void
(*)(const st::event::Event&)> >::const_iterator&)
error with iterator:
no match for operator= in i = (& allFunctions)->std::map<_Key, _Tp, _Compare, _Alloc>::erase [with _Key = int, _Tp = std::list<void (*)(const st::event::Event&)>, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::list<void (*)(const st::event::Event&)> > >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, std::list<void (*)(const st::event::Event&)> > >](i)
Any suggestions?
Upvotes: 1
Views: 3118
Reputation: 476940
Try this general associative-container erasure loop:
for (std::map<int, std::list<function> >::iterator it = allFunctions.begin();
it != allFunctions.end(); /* no increment! */ )
{
it->second.remove(listener);
if (it->second.empty()) { allFunctions.erase(it++); }
else { ++it; }
}
C++11 changes the signature and return type of member-erase
functions, and then you can have a single line it = allFunctions.erase(it);
.
Upvotes: 3