简体   繁体   English

C++ 纯虚拟 function 没有覆盖

[英]C++ pure virtual function has no overrider

I'm trying to recreate space invaders in it's most basic form.我正在尝试以最基本的形式重新创建太空入侵者。 For my game i currently have a couple of classes, one for the game functionality, one for the game engine, and one for the player.对于我的游戏,我目前有几个类,一个用于游戏功能,一个用于游戏引擎,一个用于玩家。

My current problem is that i have no overrider and no default constructor, Which i find very confusing, because in the "GameFunction.H" file on line 15 and 20 I put overrrides into the functions and it still wont work.我当前的问题是我没有覆盖程序,也没有默认构造函数,这让我很困惑,因为在第 15 行和第 20 行的“GameFunction.H”文件中,我将覆盖程序放入函数中,但它仍然无法工作。

In the player class "Player.h" I created the constructor to initialize data for the player and provided parameters in the "Game.cpp" file on line 14.在播放器 class“Player.h”中,我创建了构造函数来为播放器初始化数据,并在第 14 行的“Game.cpp”文件中提供了参数。

I don't understand why it still won't work, am I missing something?我不明白为什么它仍然不起作用,我错过了什么吗?

github link: https://github.com/JarodIking/Game-C- github 链接: https://github.com/JarodIking/Game-C-

(this is a visual studio project) (这是一个视觉工作室项目)

Heres the Game.cpp file这是 Game.cpp 文件

// Game.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include "olcConsoleGameEngine.h"
#include "GameFunction.h"
#include "Player.h"

using namespace std;

int main()
{
    GameFunction game;
    Player player = new Player(100, 20, 60);
    game.ConstructConsole(160, 160, 8, 8);
    game.Start();


    return 0;
}

Heres the GameFunction.h file这是 GameFunction.h 文件

#pragma once

class GameFunction : public olcConsoleGameEngine{
public:
    GameFunction() {

    }

private:

protected:
    bool OnUserCreate() override {
        return true;

    }

    bool OnUserUpdate(float fElapsedTime) override {
        Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');
        return true;
    }

};

And here's the Player.h file这是 Player.h 文件

#pragma once

class Player : public olcConsoleGameEngine {
public:
    //constructor
    Player(int health, int positionX, int positionY) {
        playerHealth = health;
        playerPositionX = positionX;
        playerPositionY = positionY;

    }


private:
    //private variables
    int playerHealth;
    int playerPositionX;
    int playerPositionY;

protected:
    int playerPosition(int X, int Y) {
        Fill(X, 5 * 6, Y, 5 * 6 + 10, PIXEL_SOLID, 5);

    }
};

By doing this I hope to keep track of the player position by saving the coordinates in separate variables inside the player class and color the pixels accordingly(not a simple as just X and Y of course).通过这样做,我希望通过将坐标保存在播放器 class 中的单独变量中来跟踪播放器 position 并相应地为像素着色(当然不是简单的 X 和 Y)。

I was also thinking of making a separate class for the bullets, 1 for the player and 1 for the aliens.我还想为子弹单独制作一个 class,1 个给玩家,1 个给外星人。

After that i want to make a class to create the 'cover' in space invaders, these will be the easiest之后我想制作一个 class 来创建太空入侵者的“封面”,这些将是最简单的

How do you guys recommend i go about the aliens themselves?你们如何推荐我 go 关于外星人本身? I personally thought about creating a single class for the aliens and than use a loop to create multiple objects with it and thus have more aliens on screen.我个人考虑过为外星人创建一个 class,而不是使用循环创建多个对象,从而在屏幕上显示更多外星人。 With a constructer to give each individual alien its own position, that way i can make them all move to same with perhaps a static function. Any ideas?通过构造函数为每个外星人提供自己的 position,这样我就可以让他们都移动到与 static function 相同的地方。有什么想法吗?

// User MUST OVERRIDE THESE!!
virtual bool OnUserCreate() = 0;
virtual bool OnUserUpdate(float fElapsedTime) = 0;

These functions must be overriden in every class that extends olcConsoleGameEngine.必须在每个扩展 olcConsoleGameEngine 的 class 中重写这些函数。 At the moment you have only overriden them in GameFunction.目前您只在 GameFunction 中覆盖了它们。

Also you are using the new keyword to create a player instance.您还使用 new 关键字来创建播放器实例。 This requires storage as a pointer not a plain variable.这需要存储为指针而不是普通变量。 Possible alternatives are:可能的替代方案是:

Player* player = new Player(100, 20, 60) //create a pointer to a player

or要么

Player player(100, 20 60) //create standard player variable

If you choose to create a pointer you'll need to also remember to delete it before the application closes.如果您选择创建一个指针,您还需要记住在应用程序关闭之前将其删除。

delete player

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

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