6

So I have a map myMap that I'm trying to statically initialize (has to be done this way).

I'm doing the following:

myMap = 
{
    {415, {1, 52356, 2}}, 
    {256, {356, 23, 6}},
    //...etc
};

However I'm getting the following error: "Array initializer must be an initializer list."

What is wrong with the syntax I have above?

user1855952
  • 1,515
  • 5
  • 26
  • 55
  • Check this. http://stackoverflow.com/questions/2172053/c-can-i-statically-initialize-a-stdmap-at-compile-time – Abhishek Bansal Nov 27 '13 at 11:51
  • I have checked that out and I don't think I'm having the same issue because my attempts to statically initialize a map of type map instead of map works just fine. I only get this issue when the value is an array – user1855952 Nov 27 '13 at 11:53
  • please check this http://stackoverflow.com/questions/138600/initializing-a-static-stdmapint-int-in-c – vinod Nov 27 '13 at 11:54
  • The problem only occurs when I'm trying to do it this way with an array for the value. However what I'm doing above is essentially what they say to do in the link that you posted – user1855952 Nov 27 '13 at 11:55
  • 1
    This link should be useful. http://www.cplusplus.com/forum/beginner/95335/ – Abhishek Bansal Nov 27 '13 at 11:59

2 Answers2

4

You should use array<float, 3> instead of "plain" arrray:

#include <map>
#include <array>
#include <iostream>

int main()
{
    std::map<float, std::array<float, 3>> myMap
    {
        {415, std::array<float, 3>{1, 52356, 2}},
        {256, std::array<float, 3>{356, 23, 6}}
        //...etc
    };

    /* OR 

    std::map<float, std::array<float, 3>> myMap
    {
        {415, {{1, 52356, 2}}},
        {256, {{356, 23, 6}}}
        //...etc
    };

    */

    std::cout << myMap[415][0] << " " << myMap[256][1] << " " << std::endl;

    return 0;
}
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
0

I suspect that you are trying to use Visual Studio 2012 or earlier. Support for initialization lists on std::map was not added until Visual Studio 2013.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288