STL映射迭代器复制问题

发布于 2024-09-28 21:09:58 字数 1317 浏览 2 评论 0 原文

我有一个 STL 映射,我想迭代它,但似乎无法让代码工作。代码是:

//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 ;
}

我收到的错误消息是:

错误:'iter = (((const std::map, std::allocator; >& std::_Rb_tree_iterator >::operator=(const std::_Rb_tree_iterator >&)|

所以我通常认为问题与将 iter 设置为等于它不喜欢的东西有关,因为它没有找到“operator=”的匹配项。但为什么?为什么那个任务不起作用?

编辑:

原来方法是 const,导致对 powerups 的引用也是 const,从而导致错误。我只是在阅读自己的代码方面做得很糟糕。谢谢你们!

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>::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> >'|
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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯那些不曾习惯的习惯 2024-10-05 21:09:58

您的地图名称是 poweruplist 而不是 powerups(您在 for 循环中使用此名称)。如果这不是错误的原因,那么看起来您的 for 循环位于一个通过 const 引用接受映射的函数中(或者是类的 const 成员函数)。在这种情况下,您的迭代器类型应该是 const_iterator 而不是 iterator

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.

转身泪倾城 2024-10-05 21:09:58

重新格式化错误代码以使其可读:

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

不会查看您提供的代码的错误消息。
请剪切并粘贴代码。否则就没有意义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文