C++:静态成员函数和变量-静态变量的重新定义?

发布于 2024-08-13 02:41:52 字数 1169 浏览 4 评论 0原文

我试图将单例设计模式合并到我的代码中,但我开始遇到一个奇怪的错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

早茶月光 2024-08-20 02:41:52

您需要将静态 gameState* 的定义恰好放入一个源文件中,即这一行:

gameState* gameState::state = new gameState();

如果将其放入由多个源文件包含的标头中,则每个源文件都有一个 gameState::state 这会导致链接时错误。

对于后续问题,请使用Vadakkumpadaths建议:您需要为gameState的构造函数提供定义,而不仅仅是声明。

You need to put the definition of the static gameState* into exactly one source file, i.e. this line:

gameState* gameState::state = new gameState();

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 gameStates constructor, not only a declaration.

沒落の蓅哖 2024-08-20 02:41:52

添加构造函数的定义以修复第二个链接器错误。

private:
gameState()
{
}

Add definition for your constructor to fix second linker error.

private:
gameState()
{
}
千笙结 2024-08-20 02:41:52

使用头保护宏来解决重新定义问题,并显式定义您的私有构造函数。

Use a header guard macro for the redefinition problem, and explicitly define your private constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文