我收到此错误
对第22行的`InputHandler :: InputHandler()'的未定义引用
我正在研究SDL中的InputHandler类
InputHandler.h
/*
* InputHandler.h
*
* Created on: 16 apr. 2014
* Author: JAN
*/
#ifndef INPUTHANDLER_H_
#define INPUTHANDLER_H_
#include "SDL2/SDL.h"
#include "Vector2D.h"
class InputHandler
{
public:
static InputHandler* Instance()
{
if(s_pInstance == 0)
{
s_pInstance = new InputHandler();
}
return s_pInstance;
}
void reset();
// update and clean the input handler
void update();
void clean();
// keyboard events
bool isKeyDown(SDL_Scancode key) const;
// joystick events
int getAxisX(int joy, int stick) const;
int getAxisY(int joy, int stick) const;
bool getButtonState(int joy, int buttonNumber) const;
// mouse events
bool getMouseButtonState(int buttonNumber) const;
Vector2D* getMousePosition() const;
private:
InputHandler();
~InputHandler();
InputHandler(const InputHandler&);
InputHandler& operator=(const InputHandler&);
// private functions to handle different event types
// handle keyboard events
void onKeyDown();
void onKeyUp();
// handle mouse events
void onMouseMove(SDL_Event& event);
void onMouseButtonDown(SDL_Event& event);
void onMouseButtonUp(SDL_Event& event);
// handle joysticks events
void onJoystickAxisMove(SDL_Event& event);
void onJoystickButtonDown(SDL_Event& event);
void onJoystickButtonUp(SDL_Event& event);
// member variables
// keyboard specific
const Uint8* m_keystates;
// singleton
static InputHandler* s_pInstance;
};
typedef InputHandler TheInputHandler;
#endif
InputHandler.cpp
/*
* InputHandler.cpp
*
* Created on: 16 apr. 2014
* Author: JAN
*/
#include "InputHandler.h"
#include "Game.h"
InputHandler* InputHandler::s_pInstance = 0;
bool InputHandler::isKeyDown(SDL_Scancode key) const
{
if(m_keystates != 0)
{
if(m_keystates[key] == 1)
{
return true;
}
else
{
return false;
}
}
return false;
}
void InputHandler::update()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
onKeyDown();
break;
case SDL_KEYUP:
onKeyUp();
break;
default:
break;
}
}
}
void InputHandler::onKeyDown()
{
m_keystates = SDL_GetKeyboardState(0);
}
void InputHandler::onKeyUp()
{
m_keystates = SDL_GetKeyboardState(0);
}
我是使用c ++编程并使用SDL的新手。所以这可能是一个愚蠢的错误,但如果有人能解释我会很棒!有点坚持这个现在不知道我做错了什么
答案 0 :(得分:2)
您还没有在任何地方定义默认构造函数 将其添加到cpp文件中。
你似乎也缺少其他一些成员。
答案 1 :(得分:0)
在类InputHandler中,构造函数必须是这样的:InputHandler(){}