C++:也许你知道这个陷阱?

发布于 2024-09-06 05:37:25 字数 1629 浏览 6 评论 0原文

我正在开发一款游戏。我有一个带有两个变量的标头 GameSystem (只是游戏循环之类的方法,没有类):
int mouseXint mouseY。这些在我的游戏循环中更新。现在我想从 Game.cpp 文件(由头文件和源文件构建的类)访问它们。因此,我在 Game.h#include "GameSystem.h"。执行此操作后,我收到很多编译错误。当我删除包含内容时,他当然会说:

Game.cpp:33: 错误:“mouseX”未在此范围内声明
Game.cpp:34: 错误:“mouseY”未在此范围内声明

我想要访问 mouseXmouseY 的位置。

我的所有 .h 文件都有由 Eclipse 生成的标头防护。
我正在使用 SDL,如果删除想要访问变量的行,所有内容都会完美编译并运行 (*)。

我希望你能帮助我...

这是我 #include "GameSystem.h" 时的错误日志(他引用的所有代码都有效,如 (*) 所解释的那样):

In file included from ../trunk/source/domein/Game.h:14,
                 from ../trunk/source/domein/Game.cpp:8:
../trunk/source/domein/GameSystem.h:30: error: expected constructor, destructor, or type conversion before ‘*’ token
../trunk/source/domein/GameSystem.h:46: error: variable or field ‘InitGame’ declared void
../trunk/source/domein/GameSystem.h:46: error: ‘Game’ was not declared in this scope
../trunk/source/domein/GameSystem.h:46: error: ‘g’ was not declared in this scope
../trunk/source/domein/GameSystem.h:46: error: expected primary-expression before ‘char’
../trunk/source/domein/GameSystem.h:46: error: expected primary-expression before ‘bool’
../trunk/source/domein/FPS.h:46: warning: ‘void FPS_SleepMilliseconds(int)’ defined but not used

这是尝试访问两个变量的代码:

SDL_Rect pointer;
pointer.x = mouseX;
pointer.y = mouseY;
pointer.w = 3;
pointer.h = 3;
SDL_FillRect(buffer, &pointer, 0xFF0000);

I'm developing a game. I have a header GameSystem (just methods like the game loop, no class) with two variables:
int mouseX and int mouseY. These are updated in my game loop. Now I want to access them from Game.cpp file (a class built by a header-file and the source-file). So, I #include "GameSystem.h" in Game.h. After doing this I get a lot of compile errors. When I remove the include he says of course:

Game.cpp:33: error: ‘mouseX’ was not declared in this scope
Game.cpp:34: error: ‘mouseY’ was not declared in this scope

Where I want to access mouseX and mouseY.

All my .h files have Header Guards, generated by Eclipse.
I'm using SDL and if I remove the lines that wants to access the variables, everything compiles and run perfectly (*).

I hope you can help me...

This is the error-log when I #include "GameSystem.h" (All the code he is refering to works, like explained by the (*)):

In file included from ../trunk/source/domein/Game.h:14,
                 from ../trunk/source/domein/Game.cpp:8:
../trunk/source/domein/GameSystem.h:30: error: expected constructor, destructor, or type conversion before ‘*’ token
../trunk/source/domein/GameSystem.h:46: error: variable or field ‘InitGame’ declared void
../trunk/source/domein/GameSystem.h:46: error: ‘Game’ was not declared in this scope
../trunk/source/domein/GameSystem.h:46: error: ‘g’ was not declared in this scope
../trunk/source/domein/GameSystem.h:46: error: expected primary-expression before ‘char’
../trunk/source/domein/GameSystem.h:46: error: expected primary-expression before ‘bool’
../trunk/source/domein/FPS.h:46: warning: ‘void FPS_SleepMilliseconds(int)’ defined but not used

This is the code which try to access the two variables:

SDL_Rect pointer;
pointer.x = mouseX;
pointer.y = mouseY;
pointer.w = 3;
pointer.h = 3;
SDL_FillRect(buffer, &pointer, 0xFF0000);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

霞映澄塘 2024-09-13 05:37:25

在您的 GameSystem 标头中,不要将这些变量定义为:

int mouseX;
int mouseY;

相反,您应该声明它们:

extern int mouseX;
extern int mouseY;

然后在您的 .cpp 文件之一中定义它们:

int mouseX;
int mouseY;

在头文件中定义它们的问题是编译器将尝试在包含标头的每个 .cpp 中实例化它们。

In your GameSystem header, don't define those variables as:

int mouseX;
int mouseY;

instead, you should declare them:

extern int mouseX;
extern int mouseY;

Then in one of your .cpp files you define them:

int mouseX;
int mouseY;

The problem with defining them in a header file is that the compiler will try to instantiate them in every single .cpp where you include the header.

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