-1

I am trying to use constants in a class which should be okay in c++11 but I get this warning:

 warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

The problem is that I get this many (more than 10) times for every constant I declare. This effectively fills the build messages and makes any other compiler warnings impossible to find. I would like to make it so this no longer shows up in my build messages box.

I know people like to see relevant code so here it is:

class GameState: public State
{
public:
    const Uint8 * keyStates;
    Point gameMousePos;
    int UIType;
    std::vector<UI *> UIs;
    Texture * lockingTex;
    HitBox * inGame;
    const int buttonDim = 100;
    const int buttonOffY = 70;//distance from bottom
    const int buttonOffX = 130;//distance from each other
    const int buttonTextOffY = 140;//text distance from bottom
    bool locking;
    bool noPlaceBool;
    float gameSpaceScale;
    HitBox * gameSpace;
    Texture * bkg;
    float windowRotSpeed;
    float inHandRotSpeed;
    float windowMoveSpeed;
    GameState();
    void handle_events();
    void logic();
    void render();
    void save();
    void load_save();
}
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0ctoDragon
  • 541
  • 1
  • 7
  • 20
  • Possible duplicate: http://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code – JBentley Jul 17 '14 at 22:16
  • Alternative duplicate: http://stackoverflow.com/questions/1079997/disable-specific-warnings-in-gcc – JBentley Jul 17 '14 at 22:17
  • Perhaps a silly question, but you *are* configured for C++11 compilation, right? And have you considered using static `constexpr` for those ? – WhozCraig Jul 17 '14 at 22:19
  • 3
    use `-std=c++11` to get rid of that warning – M.M Jul 17 '14 at 22:20

2 Answers2

2

The compiler warning tells you exactly what to do:

warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

By default g++ uses C++03.
To activate C++11 features you need to tell the compiler to use C++11

g++ -std=c++11 stuff.cpp

Once you are using C++11 the default features ("enabled by default") for that language will be enabled.

Martin York
  • 257,169
  • 86
  • 333
  • 562
1

The compiler is telling you that it should be static:

warning: non-static data member ...

So if you were to add the static keyword, it would solve your problem.

So change these:

const int buttonDim = 100;

With:

static const int buttonDim = 100;

And the warnings should go.

Note that was part of C++ for a very long time (only cl [Microsoft compiler] did not support it well before 2008 or so.)


As a side note, a good programmer wants to do the opposite: transform all warnings to errors to be forced to fix all warnings. I very rarely have to go around a warning and it is always rather edgy cases (like compare two floating point numbers with == or !=. So all of that to say, I would actually advice you use -Werror and always find the exact reason for a warning.

Of course, if you are working with someone else code... that's a different story. They may not want to fix their code.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156