cannot convert from ‘class std::_Tree<SOMETHING>::const_iterator’ to ‘class std::_Tree<SOMETHING>::iterator’
class NiftyEmailProgram {
private:
typedef map<string, string> NicknameMap;
NicknameMap nickname;
public:
...
void showEmailAddress(const string& nickname) const;
};
void NiftyEmailProgram::showEmailAddress(const string& nickname) const {
...
NicknameMap::iterator i = nicknames.find(nickname); // error!
if (i != nicknames.end())...
...
}
nicknames is declared as a non-const map, but showEmailAddress is a const member funcion, and inside a const member function, all non-static data members of the class const! Inside showEmailAddress, nicknames is a const map. We are trying to generate an iterator into a map we have promised not to modify. To fix the problem, we must either make i a const_iterator or we must make showEmailAddress a non-const member function.