Reputation: 131
Tiny, very simple code example that showcases the problem:
#include <string>
#include <map>
static std::map<std::string, std::map<std::string, int>> defaults = {
{ std::string("Definitely a string"), { std::string("This too"), 0 }},
};
The error? main.cpp:4:58: No matching constructor for initialization of 'std::map<std::string, std::map<std::string, int> >'
Upvotes: 4
Views: 769
Reputation: 137301
You need an extra pair of braces:
#include <string>
#include <map>
static std::map<std::string, std::map<std::string, int>> defaults = {
{ std::string("Definitely a string"), {{ std::string("This too"), 0 }}},
// ^ ^
};
It's basically the same way as initializing the outer map - the inner map needs to be initialized with an initializer list - one pair of braces - that contains initializer lists for the key-value pairs of the map - another pair of braces per element.
As Matt McNabb noted, you don't have to explicitly construct the std::string
s.
Upvotes: 8