user2011736
user2011736

Reputation: 141

Unresolved Externals - Enums within static classes

I have no idea why my code isn't working, just started learning C++ and all the namespace/header files etc..

I have a main function:

#include "stdafx.h"
#include "Game.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Game::Start();
    return 0;
}

Which calls a static function within game to start.

#include "stdafx.h"
#include "Game.h"
void Game::Start(void)
{
    _gameState = ShowingMenu;

    while(IsExiting())
    {
        switch(_gameState)
        {
            case ShowingMenu:
                ShowMenu();
                break;

        }
    }
}

Now whereever I use the enum GameState (as you will see from the header) I get:

Unresolved external symbol private static enum Game::GameState Game::GameState

This is the Game.h

#pragma once

class Game {
public:
    static void Start();
private:
    static void GameLoop();
    static bool IsExiting();
    static void ShowMenu();
    enum GameState { Uninitialized, ShowingMenu, Dealing, DealerTurn, PlayerTurn, Betting, Exiting };
    static GameState _gameState;
};

Not sure why this is not working, the only solution was to take out the whole enum.

Upvotes: 1

Views: 2593

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

Make sure you define _gameState in a single translation unit (probably within Game.cpp):

Game::GameState Game::_gameState;

The static GameState _gameState; in your class definition is only a declaration, so the member must be defined too. See C++ FAQ for more details.

Upvotes: 1

Related Questions