Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) 'operator+' not implemented in type 'std::map<int,double>' for arguments of type 'int'


Compile error.






Full error message


[C++ Error] MyUnit.cpp(6): E2094 'operator+' not implemented in type '_STL::map<int,double,_STL::less<int>,_STL::allocator<_STL::pair<const int,double> > >' for arguments of type 'int'








IDE: C++ Builder 6.0

Compiler: Borland BCC32.EXE version

Project type: Console Application

Libraries used:


const double getDouble(const std::map<int, double>& myMap, const int i)
  return myMap[i];








The cause of this compile error is that operator[] is not a const member function. This is because this member function inserts an element to the std::map. It is created to make insertion easy and to never throw. Therefore, the code above will not compile: if myMap does not have the key i, an exception has to be thrown. So, due to this it does not compile. The way to solve the above example is:


#include <map>
#include <string>
#include <cassert>
const std::map<int,std::string> GetNumberMap()
  std::map<int, std::string> numberMap;
  numberMap[0] = "Zero";
  numberMap[1] = "One";
  numberMap[2] = "Two";
  return numberMap;
template <class KeyType, class ValueType>
bool IsKeyInMap(const std::map<KeyType,ValueType>& anyMap, const KeyType& key)
  return anyMap.find(key)!=anyMap.end();
int main()
  const std::map<int, std::string> myMap(GetNumberMap());
  assert(IsKeyInMap(myMap,-2)== false);
  assert(IsKeyInMap(myMap,-1)== false);
  assert(IsKeyInMap(myMap, 0)== true);
  assert(IsKeyInMap(myMap, 1)== true);
  assert(IsKeyInMap(myMap, 2)== true);






Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict