简体   繁体   English

错误:尽管实现了所有虚函数,但仍“分配抽象类类型的对象”

[英]Error: "Allocating an object of abstract class type" despite all virtual functions being implemented

I'm receiving error "Allocating an object of abstract class type 'MainGame" even though all virtual functions have been implemented.即使已实现所有虚拟函数,我仍收到错误“正在分配抽象类类型‘MainGame’的对象”。 Below are the relevant code snippets:下面是相关的代码片段:

main.cpp主程序

#include "Gamestate_MainGame.h"

int main() {
    Game game;
    if (game.init(new MainGame))
        game.loop();
    return 0;
}

Gamestate.h游戏状态.h

#ifndef Gamestate_h
#define Gamestate_h
#include <SDL2/SDL.h>
#include "Game.h"

class GameState {
public:
    virtual bool init(Graphics* graphics, Game* game) = 0;
    virtual void quit() = 0;

    virtual void handleEvents(SDL_Event* e) = 0;
    virtual void logic() = 0;
    virtual void render() = 0;

protected:
    Game* game = NULL;
    Graphics* graphics = NULL;
};
#endif

Gamestate_MainGame.h Gamestate_MainGame.h

#ifndef Gamestate_MainGame_h
#define Gamestate_MainGame_h
#include <vector>
#include <SDL2_mixer/SDL_mixer.h>
#include "Gamestate.h"
#include "Graphics.h"
#include "Tile.h"

class MainGame : public GameState {
    // Gamestate Functions
    bool init(Graphics* graphics, Game* game);
    void quit();

    void handleEvents(SDL_Event& e);
    void logic();
    void render();

    // MainGame functions - make private?
    void makeTiles();
    void loadPositions(const int & n);
    void scrambleTiles(std::vector<Tile> t);
    void restart();

    bool isSolved();
    bool isNeighbor(const Tile& a, const Tile& b);
    int  getClickedTile(const int& x, const int& y);

private:
    Game* game = NULL;
    Graphics* graphics = NULL;

    int  clickedTile { -1 };
    int  clicks      { 0 };
    bool gameExit    { false };
    bool gameWin     { true  };
    bool catMode     { true };

    std::vector<Tile> tiles;
    std::vector<SDL_Rect> positions;

    Mix_Chunk* click = NULL;

};
#endif

Game.h游戏.h

#ifndef Game_h
#define Game_h
#include <vector>
#include <SDL2/SDL.h>
#include "Graphics.h"

class GameState;

class Game {
public:
    Game();

    bool init(GameState* state);
    void loop();

    void pushState(GameState* state);
    void popState();
    void setQuit();
private:
    bool quit { false };

    Graphics graphics;
    SDL_Event event;
    std::vector<GameState*> states;

    Uint32 new_time;
    Uint32 old_time;

    //internal loop functions
    void update();
    void render();

    void quitGame(); //will free SDL resources and perform cleanup of states
};
#endif

All MainGame functions are defined in Gamestate_MainGame.cpp.所有 MainGame 函数都在 Gamestate_MainGame.cpp 中定义。 Thank you for your help!感谢您的帮助!

virtual void handleEvents(SDL_Event* e) = 0;
                                   ^

is a different prototype than是一个不同的原型

void handleEvents(SDL_Event& e);
                           ^

You should mark all your overridden functions with override then the compiler will do the heavy lifting for you and tell you whether you messed up or not.您应该使用override标记所有被覆盖的函数,然后编译器将为您完成繁重的工作并告诉您是否搞砸了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM