开发者

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. 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>::i开发者_开发技巧terator 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> >'| 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> >&)|

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='. 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. 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). 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). In that case your type of iterator should be const_iterator and not 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜