24

I need some help understanding where this error is occurring:

warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]

This is the section of the code that it is coming from:

typedef struct Hand {
    bool straight = false;
    bool flush = false;
    bool four = false;
    bool three = false;
    int pairs = 0;
    } Hand;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Matthew Montefusco
  • 241
  • 1
  • 2
  • 3
  • remove the initializers ,, all the `= false;` and `= 0;` This is just a definition of `struct Hand`, you'll need to initialize the fields when you have an instance of `struct Hand`. Take a look at [this](https://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-accordance-with-c-programming-language-standards) – yano Jul 24 '17 at 23:26

2 Answers2

41

That's not an error, it's a warning. It tells you that you're only allowed to initialize non-static members of a struct / class starting with the C++11 standard (so called because it was published in 2011). Before that, you weren't officially allowed to by C++98 (published, you've guessed it, in 1998). Long story short, what you're doing has only become legal, official C++ in 2011. Your compiler's default seems to be the 1998 standard.

Try compiling with -std=c++11 as a command line flag (assuming you're using GCC or clang), and the warning should go away. If you're using a different compiler, there should be a flag for that as well (if it's recent enough to implement C++11).

Răzvan Cojocaru
  • 883
  • 9
  • 12
3

If you are using Code Runner extension in VS Code, you have to config the Code-runner: Executor Map. Add -std=c++17 in "code-runner.executorMap"

As @Răzvan Cojocaru has detailed above, -std=c++11 works, but I want to use the latestest version.

code runner extionsion

code runner extionsion

add -std=c++17

CN_Cabbage
  • 405
  • 3
  • 12