C++ std:映射与操作员不匹配=

C++ std:映射与操作员不匹配=,c++,map,iterator,std,C++,Map,Iterator,Std,我已经研究过类似的问题,但我根本看不出哪里出了问题。 我也尝试过:常量迭代器。但gcc无论如何都不会编译 它是关于:i=allFunctions.erase(i) void removeEventListener(常量std::字符串和类型,函数侦听器){ 如果(!hasEventListener(类型)) 返回; std::map&allFunctions=eventHandlerList[type]; std::map::迭代器i; 对于(i=allFunctions.begin();i!=

我已经研究过类似的问题,但我根本看不出哪里出了问题。 我也尝试过:常量迭代器。但gcc无论如何都不会编译

它是关于:i=allFunctions.erase(i)

void removeEventListener(常量std::字符串和类型,函数侦听器){
如果(!hasEventListener(类型))
返回;
std::map&allFunctions=eventHandlerList[type];
std::map::迭代器i;
对于(i=allFunctions.begin();i!=allFunctions.end();+i)
{
i->second.remove(监听器);
如果(i->second.empty())
{
i=所有函数。擦除(i);
}
}
if(allFunctions.empty())
eventHandlerList.erase(类型);
}
常量迭代器出错:

 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&)
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)
错误:将const std::list作为
void std::list::remove(const)的此参数
值类型&[带_Tp=void(*)(常量st::事件::事件&),_Alloc=
std::分配器,std::list::value_type=void(*)(const st::event::event&)]丢弃限定符[-fppermissive]
错误:没有匹配的函数用于调用std::map::erase(std::map::const\u迭代器&)
迭代器出错:

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)
i=(&allFunctions)中运算符=in不匹配-->std::map::erase[with _Key=int,_Tp=std::list,_Compare=std::less,_Alloc=std::分配器,std::map::iterator=std:_Rb_tree\u iterator](i)

有什么建议吗?

尝试此常规关联容器擦除循环:

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;                     }
}
for(std::map::iterator it=allFunctions.begin();
it!=allFunctions.end();/*无增量!*/)
{
它->第二个。删除(侦听器);
if(it->second.empty(){allFunctions.erase(it++);}
else{++it;}
}

C++11更改成员的签名和返回类型-
erase
函数,然后您可以有一行
it=allFunctions.erase(it)

什么是
eventHandlerList
定义为?std::map eventHandlerList;鉴于函数是函数指针的类型定义,循环逻辑在任何情况下都是错误的;你增加的
i
太多了。事实上,这个增量真的很好。甚至有人建议。我只是使用标准设置。我不认为我启用了c++11。奇怪的是,对于std::List,它工作得非常好。但是你的代码似乎是有效的。非常感谢。也许我应该用一段时间来代替。