如何使命名空间的内容可供整个项目使用而不产生多重定义符号错误?
我正在尝试制作一个相当大的视频游戏,我当前处理鼠标输入的方法是将 mouseX、mouseY 和枚举 mouseState 作为参数传递给需要了解鼠标的每个单个对象的更新函数。它相当混乱,我想通过将鼠标变量放在名为 Input 的命名空间中来使它们更加全局化,以便我可以使用 Input::mouseX 等访问它们。
就目前而言,名称空间Input存在于Input.h中(内容如下)
#pragma once
#include "allegro5\allegro.h"
#include "J_Enum.h"
namespace Input{
ALLEGRO_EVENT_QUEUE *inputQueue;
int mouseX;
int mouseY;
MOUSE_STATE mouseState;
void setUpInput();
void updateInput();
};
,并且两个成员函数在Input.cpp中定义。
#include "Input.h"
void Input::setUpInput(){...declaration
void Input::updateInput(){...''
将Input.h包含在主循环对象的头Core.h中时链接器会发出嘶嘶声,因为在它看来,Input.h 中包含的所有内容现在都是乘法定义符号。 显然我对头文件的使用有问题,因为据我所知,我在使用命名空间时没有犯任何明显的错误,并且 LNK2005 的错误代码前缀似乎暗示了链接器(?)。
如果有人能够阐明我的困境,我将不胜感激
I am attempting to make a fairly large video game, and my current method of handling mouse input is to pass mouseX, mouseY, and an enum mouseState as arguments to the update function of EVERY single object that requires knowledge of the mouse. It's fairly messy, and I want to make the mouse variables more global by putting them in a namespace called Input so that I can access them with Input::mouseX, et al.
As it stands now, The namespace Input exists in Input.h (contents below)
#pragma once
#include "allegro5\allegro.h"
#include "J_Enum.h"
namespace Input{
ALLEGRO_EVENT_QUEUE *inputQueue;
int mouseX;
int mouseY;
MOUSE_STATE mouseState;
void setUpInput();
void updateInput();
};
and the two member functions are defined in Input.cpp
#include "Input.h"
void Input::setUpInput(){...declaration
void Input::updateInput(){...''
Upon including Input.h in the main loop object's header Core.h the linker throws a hissy fit because in its eyes everything included in Input.h is now a Multiply Defined Symbol.
Clearly something is wrong with my use of header files, because to my knowledge, I haven't made any glaring mistakes in my use of namespaces and the error code prefix of LNK2005 seems to implicate the linker(?).
If anyone can possibly shed some light on my dilemma I would be most grateful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将变量声明为
extern
:Declare the variables as
extern
: