Search code examples
c++c++11uniform-initialization

Passing an initializer list to a vector based constructor


I currently have the following code

class test
{
   public:
   test(std::vector<std::string> str)
   {
   }
   test()
   {
   }
    const std::multimap<int,  std::multimap<int, test>> _var= {
                 {0x01,  {
                            {
                                0x0f, {"A", "B", "C", "D"}
                            }
                         }
                 }
        };   
};

int main()
{  
  test t;
}

Error:

main.cpp:29:9: error: could not convert '{{1, {{15, {"A", "B", "C", "D"}}}}}' from '<brace-enclosed initializer list>' to 'const std::multimap<int, std::multimap<int, test> >'
         };
         ^

I wanted to know why passing {"A", "B", "C", "D"} to std::vector<std::string> str) is failing ? Any suggestions on how I can resolve this issue ?


Solution

  • You need another pair of braces. Use:

    0x0f, {{"A", "B", "C", "D"}}
    

    Without that, the compiler tries to construct a test using the arguments "A", "B", "C", "D", as if test{"A", "B", "C", "D"}, which does not work.