static definition error

game.h:
1
2
3
4
5
6
7
8
class Game
{
public:
     enum difficulty { A,B,C }
     static difficulty getGameDiff();
private:
     static difficulty diff;
}

game.cpp:
1
2
3
4
5
6
7
#include "game.h"
difficulty Game::getGameDiff()
{
    return gameDiff;
}

difficulty Game::gameDiff = NONEDIFF;


on both definition of my static member, the compiler says 'difficulty' does not name a type
Last edited on
Since you defined difficulty inside your game class you need to access it with Game::difficulty.
Also you need to end you enum declaration with a ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Game
{
public:
  enum difficulty
  {
    NONEDIFF, A, B, C
  };
  static difficulty getGameDiff ();
  static difficulty diff;
};

Game::difficulty Game::getGameDiff ()
{
  return diff;
}

Game::difficulty Game::diff = NONEDIFF;
@Thomas1965 thanks.
actually my data member diff is a private data.

i get a compile error it says:
'Game::difficulty Game::gameDiff' is private

is the only way to define it is by making it public? if i do that it will defeat the purpose of setter/getter functions.
is the only way to define it is by making it public?
YES, if you define it inside the class.

If you want it private, you can do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum difficulty
{
  NONEDIFF, A, B, C
};

class Game
{
public:  
  static difficulty getGameDiff ();
private:
  static difficulty diff;
};

difficulty Game::getGameDiff ()
{
  return diff;
}

difficulty Game::diff = NONEDIFF;


can you explain it to me? i dont know how it differs from my code.
except from the place of definition
Last edited on
So what part that you are not fully understood about public - private class members?
i dont know why it allows the diff to be defined that way just by putting the enum outside the class.
Topic archived. No new replies allowed.