0

I declared std: map below:

std::map<std::string, std::set<unsigned char*>> FilesMap;

int InsertData(unsigned char* name)
{
  // here i try to insert pair with name above and clear std::set
   FilesMap.insert(std::pair<std::string, std::set<unsigned char*>>(std::string((char*)name), std::set<unsigned char*>()));
}

But I have many errors like:

Error 16 error C2676: binary '<': 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c: \program files (x86)\microsoft Visual Studio 10.0\vc\include\xfunctional

What am I doing wrong?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Roman
  • 1,377
  • 2
  • 11
  • 12
  • 1
    Are we to assume that you're using C++11 or that you actually have a space in your `>>` closing token? – Mark B Oct 08 '12 at 15:40
  • 3
    It compiles correctly using VS2010 in my machine. Well, assuming you have included ``, `` and ``. – Gorpik Oct 08 '12 at 15:41
  • @MarkB MSVC understands `>>` in templates for a very long time.. Long before it was standardized. – Fiktik Oct 08 '12 at 15:58

2 Answers2

3

First of all, this horribly long line

FilesMap.insert(std::pair<std::string, std::set<unsigned char*>>(std::string((char*)name), std::set<unsigned char*>()));

can be simplified if you use std::make_pair function, which will deduce template arguments.

FilesMap.insert(std::make_pair(std::string(reinterpret_cast<char*>name)), std::set<unsigned char*>()));

Second, you could make a typedef for your set so as to simplify the above line even more

typedef std::set<unsigned char*> mySetType;
std::map<std::string, mySetType>> FilesMap;
 FilesMap.insert(std::make_pair(std::string(reinterpret_cast<char*>name)), MySetType()));

And lastly, and most importantly, I believe the reason that the compiler is unable to find a suitable operator < for std::string is that you forgot to #include<string>

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • You don't address the cause of the error, he didn't want a comparison, its a template parsing error – daniel gratzer Oct 08 '12 at 15:41
  • However using the simpler form with `make_pair` removes the problematic `>>` and should still fix the problem. – Mark B Oct 08 '12 at 15:42
  • @jozefg: Actually, you're wrong and I'm right. The error is that the compiler cannot find operator < for the strings, to use as map key. Not the closing >> which MSVC handles OK for a long time now... – Armen Tsirunyan Oct 08 '12 at 15:43
0

A requirement for using std::map, is that the key type has to have an operator < . It seems you are getting an error with regards to std::string not having this operator. Make sure you have included the string header #include <string>.

Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51