Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an enum to another file? C++

Tags:

c++

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.

like image 479
P.K. Avatar asked Aug 15 '12 13:08

P.K.


2 Answers

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.

like image 133
Some programmer dude Avatar answered Oct 24 '22 18:10

Some programmer dude


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).

like image 34
Scott Hunter Avatar answered Oct 24 '22 17:10

Scott Hunter