map of classes

I am currently trying to learn some c++ coding. For my purposes I would like to employ maps, which have strings as keys and classes as members.

So I declared the class in a header file
1
2
3
4
5
6
7
  5 class property
  6 {
  7         public:
  8         int val;
  9         std::string name="default";
 10         bool vis=false;
 11 };


The main section contains this part.
1
2
3
4
 20         map<string, property> testmap;
 21 
 22         property testprop;
 23         testmap.insert(pair<string, property>('testar', testprop) );


However, when I try to compile, I get

error: invalid user-defined conversion from ‘int’ to ‘const std::basic_string<char>&’ [-fpermissive]
testmap.insert(pair<string, property>('testar', testprop) );

I do not really understand this error. I figured that you are not restricted to integers for the element?
Single quotes denote a character. Double quotes is a string.
Use "testar"
Use double quotes:

testmap.insert(pair<string, property>("testar", testprop) );
For things like this pair<string, property>('testar', testprop) I like to use embrace enclosed initialization.

testmap.insert({"testar", testprop}); or even use the emplace function

testmap.emplace("testar", testprop);
thx. The quotation marks solved the issue
Topic archived. No new replies allowed.