当前位置: 代码迷 >> 综合 >> Effective 49 learn to deciper STL-related compiler diagnostics
  详细解决方案

Effective 49 learn to deciper STL-related compiler diagnostics

热度:23   发布时间:2024-01-03 09:29:10.0

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.

  相关解决方案