0
non-static data member initializers only available with -std=c++11 or -std=gnu++11 
[enabled by default]
     int Red = 255;
non-static data member initializers only available with -std=c++11 or -std=gnu++11
[enabled by default]
     int Green = 255;
non-static data member initializers only available with -std=c++11 or -std=gnu++11 
[enabled by default]
     int Blue = 255;

Not sure why this doesn't work.

struct color {
    int Red = 255;
    int Green = 255;
    int Blue = 255;
};
user2044600
  • 15
  • 2
  • 7
  • 2
    I think you should follow your compiler's advice. – chris Oct 13 '14 at 23:35
  • When you get answers, you're supposed to pick the one that answered your question, and you click the checkmark. I see you have not done this for any of the questions you have asked. – Almo Oct 17 '14 at 04:22

4 Answers4

5

Enable c++11 or:

struct Color
{
    int Red;
    int Green;
    int Blue;
    Color() : Red(255), Green(255), Blue(255) {}
};
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
marsh
  • 2,592
  • 5
  • 29
  • 53
3

Non-static data member initializers is a feature that exists only to C++11 version of language specification. The compiler you are using does not work in C++11 mode by default. In order to switch your compiler to C++11 mode you have to specify the -std=c++11 (or -std=gnu++11) command-line option. This is what your compiler is telling you (quite unambiguously, I might add).

There's no such feature in pre-C++11 versions of the language. That's why it "doesn't work".

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

You need to enable C++ 11 or add the -std=c++11 flag to compile because what you are trying to do is only available in C++11

SemicolonExpected
  • 614
  • 2
  • 12
  • 37
0

Pre-C++11, you could only initialize non-static data members in the member initializer list of the constructor. In C++11, you can use brace-or-equal initializers as shown in your code. However, if your NSDM has a brace-or-equal initializer and appears in the member initializer list, the brace-or-equal initializer is ignored. Therefore, the following will work for both C++03 and C++11:

struct Color
{
    int Red;
    int Green;
    int Blue;
    Color() : Red(255), Green(255), Blue(255) {}
};

However, since you have a C++11 ready compiler, use -std=c++11 to enable it.