C++:也许你知道这个陷阱?
我正在开发一款游戏。我有一个带有两个变量的标头 GameSystem
(只是游戏循环之类的方法,没有类):int mouseX
和 int mouseY
。这些在我的游戏循环中更新。现在我想从 Game.cpp 文件(由头文件和源文件构建的类)访问它们。因此,我在 Game.h
中#include "GameSystem.h"
。执行此操作后,我收到很多编译错误。当我删除包含内容时,他当然会说:
Game.cpp:33: 错误:“mouseX”未在此范围内声明
Game.cpp:34: 错误:“mouseY”未在此范围内声明
我想要访问 mouseX
和 mouseY
的位置。
我的所有 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
GameSystem
标头中,不要将这些变量定义为:相反,您应该声明它们:
然后在您的 .cpp 文件之一中定义它们:
在头文件中定义它们的问题是编译器将尝试在包含标头的每个 .cpp 中实例化它们。
In your
GameSystem
header, don't define those variables as:instead, you should declare them:
Then in one of your .cpp files you define them:
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.