1

I've checked on SO already for a simple way to fix this error. I didn't get this when compiling on another computer but suddenly now it's not compiling on my PC. Here's the error I'm getting:

Error: Assigning to an array from an initializer list

And here's the code:

int maze[12][12];

void print(bool playing);

int main()
{
    printMaze(false);
    playGame();

    return 0;
}

void print(bool playing)
{
    if (!playing) maze = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
        {2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
        {1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
        {1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
        {1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
        {1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
        {1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
        {1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    };
}

It might also be worth mentioning that I get a warning on the same line:

Warning: Extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

I know that clearly means I have to use one of these two to use extended initializer lists, but have no idea what to do to resolve the matter.

Edit:

Having g++ follow the C++11 ISO C++ language standard in the settings removes the warning, but not the error.

Dragonphase
  • 319
  • 2
  • 6
  • 15

2 Answers2

1

What do your compilations steps look like? The warning is fairly clear: you are trying to use a feature that requires -std=c++11 or -std=gnu++11, and although that is apparently enabled by default, it is possible that you have overridden it (i.e. explicitly turned it off) somehow. You should examine your compilation process closer and make sure you aren't preventing that feature from being allowed.

pattivacek
  • 5,617
  • 5
  • 48
  • 62
  • I'll be honest, I don't really know where to look for my compiler process. I'm using MinGW to compile on Code::Blocks. – Dragonphase Sep 16 '13 at 18:43
  • You are going to need to find that information. Somewhere there must be an option to set/modify your compiler flags. I'd look out for anything that looks like `-std=c++98` or something similar, i.e. not the two options the warning mentioned. The full list for gcc 4.8.1 (assuming that's what you're actual compiler is) can be found here: http://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/C-Dialect-Options.html – pattivacek Sep 16 '13 at 18:49
  • I can't find anything related in my compiler flags list. Also, the fact I'm getting this error tells me that my code won't compile the same way on different machines; are there no alternative ways to resolve this without changing compiler settings? – Dragonphase Sep 16 '13 at 18:52
  • If you use the same version of the same compiler with the same flags, you should expect the same results (unless you are linking with other libraries that aren't the same). I suspect that there is something different in your configuration between your two boxes, though. The alternate way in this case is to not use that feature. – pattivacek Sep 16 '13 at 19:01
  • That's not really helpful considering I need to use that feature. – Dragonphase Sep 16 '13 at 19:15
  • Is the other part of my response helpful? Can you compare compiler versions and flags between the two machines that exhibit the discrepancy? – pattivacek Sep 16 '13 at 19:22
  • I can't access the first machine at this time. – Dragonphase Sep 16 '13 at 19:30
  • Can you specifically add `-std=c++11` or `-std=gnu++11` to the end of the compiler flags for the machine you do have access to? I'm concerned that something else will nullify it, but it's worth a shot if you haven't tried that. – pattivacek Sep 16 '13 at 20:06
  • I don't know where to enter this. See my main post, where I state I have added -std=c++11 from a list of selection options; -std=gnu++11 was not there. This removes the warning but the error still persists. – Dragonphase Sep 16 '13 at 22:42
  • I think what you are trying to do isn't possible. I wasn't sure how far the c++11 standard went, but I think we were both under the impression that it could do something that it can't. See these other posts: http://stackoverflow.com/a/15603282/289099 and http://stackoverflow.com/questions/10694689/how-to-initialize-an-array-in-c-objects. Honestly, your question is basically a duplicate of the first. – pattivacek Sep 18 '13 at 13:22
1

A workaround is to use the old-style C function memcpy. This will work with older compilers.

int maze[12][12];

void printMaze(bool playing);

int main()
{

        printMaze(false);
        playGame();
        return 0;
}

void printMaze(bool playing)
{

        static int maze1[12][12] = {
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
            {2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
            {1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
            {1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
            {1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
            {1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
        }; 

        if (!playing) memcpy(maze, maze1, 12*12*sizeof(int));
}
pattivacek
  • 5,617
  • 5
  • 48
  • 62
user2784234
  • 451
  • 3
  • 9