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> >'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With