Typedefs inside classes

P

Pelle Beckman

Hi,

Reading X. Meng's post on embedded classes I came to
think of a similar problem I found myself having.

void GamestateToString(class GameApp::TGamestate);

class GameApp {
typedef enum {
STATE_1,
STATE_2
} TGamestate;

[...]
};

This won't compile since I'm not allowed to
define GamestateToString()'s arguments like
that. Why?

In fact, it seems like I can't access GameApp::TGamestate at all,
not even for simple variables, ie.

GameApp::TGamestate state;
state = STATE_1;

-- Pelle
 
J

John Carson

Pelle Beckman said:
Hi,

Reading X. Meng's post on embedded classes I came to
think of a similar problem I found myself having.

void GamestateToString(class GameApp::TGamestate);

class GameApp {
typedef enum {
STATE_1,
STATE_2
} TGamestate;

[...]
};

This typedef is unnecessary --- a hangover from C. You can make it:

class GameApp {
enum TGamestate{
STATE_1,
STATE_2
};

[...]
};
This won't compile since I'm not allowed to
define GamestateToString()'s arguments like
that. Why?

You are doing three things wrong:

1. You should be declaring GamestateToString after, not before, the GameApp
class.

2. Anything in a class is private by default. You need to specify public:
before TGamestate.

3. You are using the wrong syntax for function parameters. Rather than

void GamestateToString(class GameApp::TGamestate);

try

void GamestateToString(GameApp::TGamestate state);
 
R

Rolf Magnus

Pelle said:
Hi,

Reading X. Meng's post on embedded classes I came to
think of a similar problem I found myself having.

void GamestateToString(class GameApp::TGamestate);

class GameApp {
typedef enum {
STATE_1,
STATE_2
} TGamestate;

[...]
};

This won't compile since I'm not allowed to
define GamestateToString()'s arguments like
that. Why?

Because TGamestate is private in class GameApp, and because the class is
defined after the declaration of GamesateToString, so the compiler doesn't
know anything about GameApp::TGamestate yet.
In fact, it seems like I can't access GameApp::TGamestate at all,
not even for simple variables, ie.

GameApp::TGamestate state;
state = STATE_1;

What does "can't access" mean? What happens if you try?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top