views:

57

answers:

2

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!

+3  A: 

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.

Naveen
Whoops, typo on my part. Fixed, thanks!
Lewis
you mean you are not getting compiler error now or was it a typo while posting the question?
Naveen
Typo while posting the question. powerups is a member of the class Poweruplist, and I got confused :D
Lewis
Changing it to const_iterator fixes the problem, but means I can't change anything pointed to by the iterator, which I do need to do. What's weird though is that as far as I know powerups isn't a const reference. powerups is a private member of the same class this function is a method of. Any ideas?
Lewis
Is the member function `const`? or can you post the signature of the method?
Naveen
@Lewis, if the function containing this code is declared `const` then any members accessed will also be `const`.
Mark Ransom
Your absolutely right, and I was being blind. Turns out the method was const after all, and I was reading the wrong method. Thanks so much!
Lewis
A: 

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.

Martin York