【发布时间】:2013-11-29 12:49:11
【问题描述】:
所以我的理解是,要在 c++ 中创建一个抽象类,您必须在该类中创建一个,只有一个纯虚方法。在我的代码中,我创建了一个抽象 GameObject 类,它由我的 Player 类继承,但问题是我的 Player.cpp 中出现错误,提示错误 LNK2001: unresolved exrenal symbol "public:virtual void__thiscall GameObject::Load(void)" (?Load@GameObject@@UAEXXZ) 除了初始化之外的每个方法,当我将它们全部设置为 0 时,这会得到修复,我只是想知道为什么
// Abstract class to provide derived classes with common attributes
#include <SDL.h>
#include "AnimationManager.h"
#include "Debug.h"
#include "InputHandler.h"
class GameObject
{
public:
virtual void Initialise() = 0;
virtual void Load();
virtual void HandleEvents();
virtual void Update();
virtual void Draw();
Vector2D* position;
int currantFrame;
SDL_Renderer* renderer;
float speed;
bool alive;
};
#include "GameObject.h"
class Player : public GameObject
{
public:
virtual void Initialise();
virtual void Load();
virtual void HandleEvents();
virtual void Update();
virtual void Draw();
Player(SDL_Renderer* r);
~Player();
};
#include "Player.h"
Player::Player(SDL_Renderer* r)
{
renderer = r;
}
Player::~Player()
{
}
void Player::Initialise()
{
position = new Vector2D(10, 10);
currantFrame = 0;
}
void Player::Load()
{
TheAnimationManager::Instance()->load("Assets/circle.png", "player", renderer);
}
void Player::HandleEvents()
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_a:
DEBUG_MSG("A Pressed");
position->m_x -= 10;
break;
case SDLK_d:
DEBUG_MSG("D Pressed");
position->m_x += 10;
break;
}
break;
}
}
}
void Player::Update()
{
Vector2D* p = TheInputHandler::Instance()->GetMousePosition();
DEBUG_MSG(p->GetY());
DEBUG_MSG(p->GetX());
currantFrame = int(((SDL_GetTicks() / 100) % 4));
}
void Player::Draw()
{
TheAnimationManager::Instance()->Animate("player", (int)position->GetX(), (int)position->GetY(), 90, 82, 0, currantFrame, renderer, SDL_FLIP_NONE);
}
【问题讨论】:
-
你得到什么错误?
-
1) 请发布确切的错误和相应的行。 2) 请确保您的抽象类中的 each 抽象方法在您的“播放器”类中具有相应的实现(看起来您已经在这样做了)。 3)这是一个很好的链接:Abstract base class example
-
编辑了我的问题和代码