我目前有以下代码

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;
}

错误:
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> >'
         };
         ^

我想知道为什么将{“A”,“B”,“C”,“D”}传递给std::vector<std::string> str)失败了吗?关于如何解决此问题的任何建议?

最佳答案

您需要另外一对牙套。使用:

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

否则,编译器将尝试使用test参数构造"A", "B", "C", "D",就像test{"A", "B", "C", "D"}一样不起作用。

关于c++ - 将初始化列表传递给基于 vector 的构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55837133/

10-11 23:01
查看更多