Private static data member in Cpp.. can only be initialized at its definition, yet invalid in-class initialization?
Asked Answered
C

3

6

Initializing in the header file i get the following error:

invalid in-class initialization of static data member of non-integral type 'bool [8]'

if i try to initialize in the .cpp, i get:

'bool Ion::KeyboardInput::key [8]' is a static data member; it can only be initialized at its definition

Heres the header:

enum MYKEYS {
    KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_W, KEY_S, KEY_A, KEY_D
};

class KeyboardInput
{
public:
    KeyboardInput();
    ~KeyboardInput();
    static void getKeysDown(ALLEGRO_EVENT ev);
    static void getKeysUp(ALLEGRO_EVENT ev);
    static bool getKey(int keyChoice);

private:
    static bool key[8] = {false, false, false, false, false, false, false, false};
};
Cady answered 18/6, 2012 at 23:17 Comment(2)
Include your attempt to initialize it in the .cppAstatic
Agreed with @Astatic -- works fine here, so you must have had invalid syntax in your attempt at defining in your .cpp.Leong
I
6

The first error message informs you that it is improper to initialize the static member variable in the header file. The second error message implies you tried to initialize the static member key in your constructor.

A static class member variable needs to be declared inside the class (without initialization) and then defined outside of the class in the .cpp file (sort of like a global variable, except the variable name has the class name included with it).

bool KeyboardInput::key[8];

The definition of the variable may include an initializer. Since you were initializing it to all false, the above definition in your .cpp file is sufficient.

A static class member variable is not too different from a global variable, except that it is scoped by the class name, and can be protected to only be accessible by members of the class (with private), the class's immediate subclasses (with protected), or the class's friends.

Intramural answered 18/6, 2012 at 23:24 Comment(0)
A
6

You have to declare the statics in the .h file and assign values to it in the .cpp file. Something like this,

_header.h

class KeyBoardInput{

public:
   KeyboardInput();
   ....
private:
   static bool key[8];
};

_header.cpp

\#include<"_header.h">

bool KeyBoardInput::key[8] = {false, false, false, false, false, false, false, false};

Other than in the cpp file you cannot initialize a static variable anywhere... It does not belong to a specific object.. So you have to initialize outside the class (in cpp file) so that all the objects can share it.

Assess answered 18/6, 2012 at 23:26 Comment(0)
B
0

Try this:

enum MYKEYS {
    KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_W, KEY_S, KEY_A, KEY_D
};

class KeyboardInput
{
public:
    KeyboardInput();
    ~KeyboardInput();
    static void getKeysDown(ALLEGRO_EVENT ev);
    static void getKeysUp(ALLEGRO_EVENT ev);
    static bool getKey(int keyChoice);

private:
    static bool key[8];
};
bool KeyboardInput::key = {false, false, false, false, false, false, false, false};

The last line should really be put in the .cpp file since all "code" should go in a cpp file.

Burma answered 18/6, 2012 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.