简体   繁体   English

STL映射迭代器复制问题

[英]Problem with STL map iterator copying

I have an STL map that I want to iterate through, and can't seem to get the code to work. 我有一个我想要迭代的STL映射,似乎无法让代码工作。 The code is: 代码是:

//PowerupInfo is a struct defined in this class's header file
std::map<std::string, PowerupInfo> powerups;

...populate powerups

std::map<std::string, PowerupInfo>::iterator iter;
for (iter = powerups.begin(); iter != powerups.end(); iter++) {
    return iter->second.type ;
}

The error message I get is: 我得到的错误信息是:

error: no match for 'operator=' in 'iter = (((const std::map<std::string, PowerupInfo, std::less<std::string>, std::allocator<std::pair<const std::string, PowerupInfo> > > )((const PowerupList )this)) + 24u)->std::map<_Key, _Tp, _Compare, _Alloc>::begin with _Key = std::string, _Tp = PowerupInfo, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, PowerupInfo> >'| 错误:'iter =(((const std :: map <std :: string,PowerupInfo,std :: less <std :: string>,std :: allocator <std :: pair <中的'operator ='不匹配const std :: string,PowerupInfo >>> )((const PowerupList )this))+ 24u) - > std :: map <_Key,_Tp,_Compare,_Alloc> :: begin with _Key = std :: string,_Tp = PowerupInfo,_Compare = std :: less <std :: string>,_ Alloc = std :: allocator <std :: pair <const std :: string,PowerupInfo >>'| note: candidates are: std::_Rb_tree_iterator<std::pair<const std::string, PowerupInfo> >& std::_Rb_tree_iterator<std::pair<const std::string, PowerupInfo> >::operator=(const std::_Rb_tree_iterator<std::pair<const std::string, PowerupInfo> >&)| 注意:候选者是:std :: _ Rb_tree_iterator <std :: pair <const std :: string,PowerupInfo >>&std :: _ Rb_tree_iterator <std :: pair <const std :: string,PowerupInfo >> ::: operator =(const std :: _ Rb_tree_iterator <std :: pair <const std :: string,PowerupInfo >> &&)|

So I would normally assume that the problem has to do with setting iter equal to something it doesn't like, as it's not finding a match for 'operator='. 所以我通常认为问题与设置iter等于它不喜欢的东西有关,因为它找不到'operator ='的匹配。 But why? 但为什么? Why wouldn't that assignment work? 为什么这个任务不起作用?

EDIT: 编辑:

Turns out the method WAS const, causing the reference to powerups to be const as well, causing the error. 结果是WAS const方法,导致对powerups的引用也是const,导致错误。 I was just doing a bad job reading my own code. 我只是在读我自己的代码做得很糟糕。 Thanks guys! 多谢你们!

Your map name is poweruplist not powerups (You are using this name in the for loop). 你的地图名称是poweruplist而不是powerups (你在for循环中使用这个名字)。 If this is not the cause of the error, then it looks like you are for loop is in a function which accepts the map by const reference (or is a const member function of a class). 如果这不是错误的原因,那么看起来你是for循环是在一个函数中,它通过const引用接受映射(或者是类的const成员函数)。 In that case your type of iterator should be const_iterator and not iterator . 在这种情况下,您的迭代器类型应该是const_iterator而不是iterator

Reformatting error code to make it readable: 重新格式化错误代码以使其可读:

error: no match for 'operator=' in 
  'iter = 
    ((
      (const std::map<std::string, PowerupInfo>*)((const PowerupList*)this)
     ) 
      + 24u
    )->std::map<std::string, PowerupInfo>::begin()'

Does not look the error message to the code you supplied. 不查看您提供的代码的错误消息。
Please cut and past the code. 请剪切并通过代码。 Otherwise it is meaningless. 否则就没有意义了。

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

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