2

(我对 C++ 很陌生,所以希望这只是一个菜鸟错误)

我的代码有问题,我有一个需要许多属性的类“播放器”,我试图通过使用抽象类来给它:

//player.h

class Player : public IUpdate, public IPositionable, public IMoveable, public IDrawable
{
public:
    Player(void);
    SDL_Rect get_position();
    void move(Uint32 dTime);
    void update(Uint32 dTime);
    void show(SDL_Surface* destination);
    ~Player(void);
private:
    SDL_Surface texture;
    int x, y;
};

我正在重写纯虚函数:

//Player.cpp
Player::Player(void)
{
}

SDL_Rect Player::get_position()
{
    SDL_Rect rect;
    rect.h = 0;
    return rect;
}

void Player::move(Uint32 dTime)
{

}

void Player::update(Uint32 dTime)
{
    move(dTime);
}

void Player::show(SDL_Surface* destination)
{
    apply_surface(x, y, &texture, destination, NULL);
}

Player::~Player(void)
{
}

但是我不断收到编译错误:C2259: 'Player' : cannot instantiate abstract class

据我所知,纯虚函数应该被覆盖,我的谷歌搜索告诉我,这会使 Player 非抽象,但 Player 似乎仍然是抽象的。

编辑:纯虚函数:

class IPositionable
{
public:
    virtual SDL_Rect get_position() = 0;
private:
    int posX, posY;
};

class IUpdate
{
public:
    virtual void update (Uint32 dTime) = 0;
};

class IMoveable
{
public:
    int velX, velY;
    virtual void move(Uint32 dTime) = 0;
};

class IDrawable
{
public:
    virtual void show() = 0;
private:
    SDL_Surface texture;
};

class IHitbox
{
    virtual void check_collsion() = 0;
};

class IAnimated
{
    virtual void next_frame() = 0;
    int state, frame;
    int rows, columns;
};
4

5 回答 5

4

你的问题在这里:

class IDrawable
{
public:
    virtual void show() = 0;
};

void Player::show(SDL_Surface* destination)
{
    apply_surface(x, y, &texture, destination, NULL);
}

注意Player::show(SDL_Surface* destination)不覆盖纯虚方法IDrawable::show()
为了覆盖该方法,您需要在派生类中使用完全相同的函数签名(只允许协变返回类型
您现在拥有的是show()在派生类中命名的方法,它隐藏了在基类中命名的方法show(),它不会覆盖它。由于您没有为类编译器的所有纯虚函数提供定义,Player因此正确地告诉您它是一个抽象类。

于 2012-09-15T12:22:43.113 回答
1

您可能没有覆盖其中一个基的纯虚函数,而是声明并定义了一个具有略微不同签名的函数,如下所示:

struct base {
    virtual void foo(double d) = 0;
};

struct derived: base {
    // does not override base::foo; possible subtle error
    void foo(int i);
}

您可能想通过审查来仔细检查您的代码。如果您使用的是 C++11,您将能够标记您的函数override以捕获此类错误。

于 2012-09-15T11:34:12.837 回答
0

抽象类是抽象的 - 即没有定义但只是声明的东西。

您需要定义所有这些方法。由于我没有这些类的声明,因此我无法就您缺少的方法向您提供建议。

于 2012-09-15T11:19:13.043 回答
0

在 C++ 中,函数不是虚拟的,除非它是专门编写的:

virtual void move(Uint32 dTime);

纯虚函数定义如下:

virtual void move(Uint32 dTime) = 0;

您继承的“接口”(请注意,这是多重继承.. C++ 与类的接口没有区别)具有您未实现的纯虚函数,从而使您的类抽象。

于 2012-09-15T11:20:21.717 回答
0

当然,这是由于缺少对纯虚函数的覆盖造成的——也许只是一个细微的签名差异。

我希望编译器会告诉您哪个函数仍未被覆盖,例如(vc9):

C2259: 'Player' : cannot instantiate abstract class
due to following members:
'void IUpdate::update(void)' : is abstract
virtualclass.cpp(3) : see declaration of 'IUpdate::update'

如果您的编译器没有报告这一点,您可以通过删除继承的接口来检查。

于 2012-09-15T11:42:51.917 回答