标准:映射与运算符=不匹配
std:map no match for operator=
我看过类似的问题,但我根本看不出出了什么问题。我也试过::const_iterator。但是 gcc 不会以任何一种方式编译。
它是关于 : 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);
}
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&)
迭代器错误:
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)
有什么建议吗?
试试这个通用的关联容器纠删循环:
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 更改成员erase
函数的签名和返回类型,然后您可以it = allFunctions.erase(it);
单行。
相关文章:
- 在使用累加时,C++中的运算符+不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- 不显示匹配运算符
- 映射迭代器与运算符不匹配
- 与"运算符<<"C++不匹配
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与标准中的"运算符<<"不匹配
- 运算符不匹配*=
- 不断收到错误" 与"运算符>>不匹配";
- 编译我的 3 个文件时,我收到错误,说"运算符="不匹配
- C++ 模板与运算符<不匹配
- 与"运算符="不匹配
- 代码块编译错误与运算符<<不匹配
- 错误:运算符 [] 不匹配。在比较列表中的 int 和 int 时<int>,
- 解决错误:运算符 [] 不匹配
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与“运算符 []”不匹配
- C++ 设置:不匹配 - 运算符