user2324350
user2324350

Reputation: 37

Initialization of map with a list as value

I am trying to initialize a map that contains a list

map<string, list<int>> firstNamesMap = {{"name1", new list<int>}};

I get the following error:

error: could not convert ‘{{"name1", (operator new(8), (<statement>, ((std::list<int>*)<anonymous>)))}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<std::basic_string<char>, std::list<int> >’
 map<string, list<int>> firstNamesMap = {{"name1", new list<int>}};
                                                                 ^

I was originally trying to initialize a much larger map with list<Data *> instead of list<int>, in which "Data" is a simple class declared earlier. It produces the same error either way though.

Not sure if it matters, but I'm compiling with g++ in Cygwin.

Upvotes: 1

Views: 583

Answers (1)

jfMR
jfMR

Reputation: 24788

new list<int> results in a pointer to a list<int> (i.e., list<int> *). However, looking at the mapped type of your map:

map<string, list<int>> 
            ^^^^^^^^^

What you actually need is a list<int>, not a list<int>*.


Try with list<int>() instead of new list<int> when initializing the map:

map<string, list<int>> firstNamesMap = {{"name1", list<int>()}};

Upvotes: 3

Related Questions