繁体   English   中英

std:map不匹配operator =

[英]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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM