0

I have the code

void switchstate(gamestates state) --line 53
{ --line 54
    switch(state)
    case state_title:
        title();
        break;
    case state_about:
        break;
    case state_game:
        break;
    case state_battle:
        break;
}

enum gamestates
{
state_title, state_about, state_game, state_battle,
};


int main( int argc, char* args[] )
{
gamestates currentstate = state_title;
startup();
load_resources();
switchstate(currentstate); --line 169
return 0;
}

and when I try to compile I get the errors:

\main.cpp:53: error: 'gamestates' was not declared in this scope
\main.cpp:54: error: expected ',' or ';' before '{' token
\main.cpp: In function 'int SDL_main(int, char**)':
\main.cpp:169: error: 'switchstate' cannot be used as a function

I've never used enumerations before so I'm confused on what's not working.

Austin Gayler
  • 4,038
  • 8
  • 37
  • 60

6 Answers6

3

Generally, errors of the "<symbol> not in scope" means the compiler hasn't seen <symbol> yet. So move the declaration of gamestates to before void switchstate(...), either via an earlier #include or just moving it up in the file.

C and C++ compile from top to bottom, so symbols must be declared before they are used.

MSN
  • 53,214
  • 7
  • 75
  • 105
2

Move the declaration of the enum so it's above the switchstate function. That should do the trick. C++ is very particular about the order things are declared.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
0

Move the enum gamestates line up in the file, before switchstate.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
0

Try moving definition of gamestates above the switchstate function definition.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
cristobalito
  • 4,192
  • 1
  • 29
  • 41
0

You may want to define the enum before the switchstate function.

Casey
  • 12,070
  • 18
  • 71
  • 107
0

In C++, you have to declare all of your types before you can refer to them. Here, you're declaring your enum after the switchstate function, so when the C++ compiler reads switchstate, it sees you refer to a type it doesn't know yet, and makes an error. If you move the enum declaration before switchstate, you should be fine.

In general, you should put declarations at the top of your file, or in seperate header files which you include at the top of the file.

Michael Madsen
  • 54,231
  • 8
  • 72
  • 83