我正在为我的游戏设置状态系统,并遇到有关枚举的问题。我要做的是定义APP_STATE枚举的实例并在不同的翻译单元之间共享。
代码:
// APP_STATE.h
#pragma once
enum class APP_STATE : signed char { RUNNING = 2, LOAD = 1, EXIT = 0, FORCE_QUIT = -1 };
// Source.cpp
#include "APP_STATE.h"
APP_STATE appState = APP_STATE::RUNNING;
// Other.cpp
#include "APP_STATE.h"
namespace other {
extern APP_STATE appState;
void foo () {
appState = APP_STATE::EXIT; // causes a LNK1120 and LNK2001 error, unresolved extrernal symbol
}
}
答案 0 :(得分:0)
您已定义两个 APP_STATE
::appState
:它位于全局命名空间中,在APP_STATE.h
中声明并在Source.cpp
中定义。other::appState
:它在名称空间other
中,在Other.cpp
中声明但从未定义,因此出错。在Other.cpp
中,您应该将extern APP_STATE appState;
的声明移到命名空间之外:
// Other.cpp
#include "APP_STATE.h"
extern APP_STATE appState;
namespace other {
void foo () {
appState = APP_STATE::EXIT;
}
}