C++:静态成员函数和变量-静态变量的重新定义?
我试图将单例设计模式合并到我的代码中,但我开始遇到一个奇怪的错误:
main.obj : error LNK2005: "private: static class gameState * gameState::state" (?state@gameState@@0PAV1@A) already defined in gameState.obj
如果您不熟悉单例模式,它基本上用于在整个程序中仅强制执行某个对象的 1 个实例。 这是相关代码: gameState.h:
class gameState
{
public:
static gameState* Instance() {return state;}
.
.
.
private:
gameState();
static gameState* state;
};
gameState* gameState::state = new gameState();
现在我只是在 main.cpp 文件中使用该对象的实例:
gameState *currState = gameState::Instance();
.
.
.
for_each(currState->getHumanPieces().begin(),currState->getHumanPieces().end(), drawPieces);
似乎我正在尝试重新定义 gameState::state,但无法弄清楚为什么......可以帮助任何人吗?
这解决了这个问题,但仍然存在一个错误,我之前实际上没有发布该错误,因为我认为它只是另一个错误的一部分:
error LNK2019: unresolved external symbol "private: __thiscall gameState::gameState(void)" (??0gameState@@AAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'private: static class gameState * gameState::state''(void)" (??__E?state@gameState@@0PAV1@A@@YAXXZ)
关于如何修复该错误有什么好的建议吗?
谢谢你们两位,已经解决了:D
I was trying to incorporate the Singleton design pattern into my code, but I started getting a weird error:
main.obj : error LNK2005: "private: static class gameState * gameState::state" (?state@gameState@@0PAV1@A) already defined in gameState.obj
If you're not familiar with the singleton pattern, it is basically used to enforce only 1 instance of a certain object in the entire program.
Here is the relevant code:
gameState.h:
class gameState
{
public:
static gameState* Instance() {return state;}
.
.
.
private:
gameState();
static gameState* state;
};
gameState* gameState::state = new gameState();
and right now I'm just using the instance of that object in the main.cpp file:
gameState *currState = gameState::Instance();
.
.
.
for_each(currState->getHumanPieces().begin(),currState->getHumanPieces().end(), drawPieces);
It would seem I am trying to redefine gameState::state, but can't figure out why... help anyone?
that solved that, but one error still remains, which I didn't actually post before as I thought it was just part of the other one:
error LNK2019: unresolved external symbol "private: __thiscall gameState::gameState(void)" (??0gameState@@AAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'private: static class gameState * gameState::state''(void)" (??__E?state@gameState@@0PAV1@A@@YAXXZ)
any good tip on how to fix that one as well?
Thanks both of you, its fixed :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将静态
gameState*
的定义恰好放入一个源文件中,即这一行:如果将其放入由多个源文件包含的标头中,则每个源文件都有一个
gameState::state
这会导致链接时错误。对于后续问题,请使用Vadakkumpadaths建议:您需要为
gameState
的构造函数提供定义,而不仅仅是声明。You need to put the definition of the static
gameState*
into exactly one source file, i.e. this line:If you put it in a header which is included by multiple source files, each has a definition of
gameState::state
which leads to errors at link-time.For the follow-up problem, use Vadakkumpadaths advice: you need to provide a definition for
gameState
s constructor, not only a declaration.添加构造函数的定义以修复第二个链接器错误。
Add definition for your constructor to fix second linker error.
使用头保护宏来解决重新定义问题,并显式定义您的私有构造函数。
Use a header guard macro for the redefinition problem, and explicitly define your private constructor.