5

first time posting here, I'm a beginner in C++ programming, learning it mostly because I want to know it because it was always interesting, as how does this work, etc.
I'm trying to make a simple game, using SFML 2.0, my question is:
I have an enum, for example:

    enum GameState
    {
        Menu,
        Battle,
        Map,
        SubMenu,
        Typing
    };

So, I want to make a variable of that kind, using

    GameState State = Menu;

And then, pass it to another file as

    extern GameState State;

But I get error

    error: 'GameState' does not name a type

How to pass an enum to another file? I'm trying to do it by making it as a global variable in main.cpp and then including it in header of another file.

P.K.
  • 777
  • 1
  • 7
  • 18
  • 1
    Please state in what file each code block is in. – Ólafur Waage Aug 15 '12 at 13:03
  • 1
    wherever the `extern` declaration is, must able to see the declaration of the enum (so it knows `GameState` names a type). Typically that declaration should be in a header (`.h` or `.hpp` file) that you can `#include` – Useless Aug 15 '12 at 13:04
  • When you say pass `extern GameState State` to another file, is `extern GameState State` in the source file or the destination file? – czchlong Aug 15 '12 at 13:04
  • if possible cook it down to a minimum self-contained (not) compiling example to allow others to reproduce what you have tried. – moooeeeep Aug 15 '12 at 13:05
  • @ÓlafurWaage First part, the enum declaration and the variable State are in main.cpp, the rest is in another file .hpp – P.K. Aug 15 '12 at 13:07
  • @Useless let me try that then – P.K. Aug 15 '12 at 13:07

2 Answers2

11

You have to put the enum in a header file, and use #include to include it in the source file.

Something like this:

File gamestate.h:

// These two lines prevents the file from being included multiple
// times in the same source file
#ifndef GAMESTATE_H_
#define GAMESTATE_H_

enum GameState
{
    Menu,
    Battle,
    Map,
    SubMenu,
    Typing
};

// Declare (which is different from defining) a global variable, to be
// visible by all who include this file.
// The actual definition of the variable is in the gamestate.cpp file.
extern GameState State;

#endif // GAMESTATE_H_

File gamestate.cpp:

#include "gamestate.h"

// Define (which is different from declaring) a global variable.
GameState State = Menu;  // State is `Menu` when program is started

// Other variables and functions etc.

File main.cpp:

#include <iostream>
#include "gamestate.h"

int main()
{
    if (State == Menu)
        std::cout << "State is Menu\n";
}

Now the global variable State is defined in the file gamestate.cpp, but can be referenced in all source files that includes gamestate.h thanks to the extern declaration in that file. And more importantly, the enum type GameState is also defined when you include gamestate.h in a source file, so that error you have about it not being defined will go away.

For the difference between a declaration and a definition, see e.g https://stackoverflow.com/a/1410632/440558.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

The problem seems to be that you've defined what GameState means in one file, but 2 files need to know the definition. The typical way of accomplishing this is to create a header file (with a .h extension) that gets included (using #include) in both of the source code files (.cpp, most likely), so that it appears in both. This is better than just copying and pasting the definition (to use it elsewhere you just need the #include statement; if the definition changes, you just change it in the .h file, and every file that includes it gets the changes when recompiled).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101