c++ - 不允许抽象类类型“mazeGenerator”的 C++ 对象:纯虚函数“olcGameEngine::OnUserUpdate”没有覆盖器
问题描述
因此,自从我尝试学习 c++ 以来,我一直在关注 oneonecoders 教程,但是我被卡住了。我制作的“mazeGenerator”类不会被实例化。像我这样的其他问题在 onUserUpdate 函数上使用 const 作为解决方案,但是,这不起作用,我还有其他问题可以解决,但我找不到解决方案。
非常感谢您!
解决方案可能就在我的眼皮底下,但我就是找不到
这是我到目前为止编写的代码:
// mazeGenerater.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#pragma once
#include <iostream>
#include <stack>
using namespace std;
#include "olcConsoleEngine.h"
class mazeGenerator : public olcConsoleGameEngine {
public:
mazeGenerator(){
m_sAppName = L"MAZE";
}
private:
int m_nMazeWidth;
int m_nMazeHeight;
int *m_maze;
enum{
CELL_PATH_N = 0x01,
CELL_PATH_E = 0x02,
CELL_PATH_S = 0x04,
CELL_PATH_W = 0x08,
CELL_VISITED = 0x10,
};
int m_nVisitedCells;
stack<pair<int , int>> m_stack; // (x, y) coordinate pairs
protected:
virtual bool OnUserCreate() {
//initialize alogrithm
m_nMazeWidth = 40;
m_nMazeHeight = 25;
//allocate maze memory
m_maze = new int[m_nMazeWidth * m_nMazeHeight];
memset(m_maze, 0x00, m_nMazeWidth * m_nMazeHeight);
//push cells to stack
m_stack.push(make_pair(0, 0));
m_maze[0] = CELL_VISITED;
m_nVisitedCells = 1;
return true;
}
virtual bool onUserUpdate(float fElppasedTime) {
// draw stuff
Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');
for (int x = 0; x < m_nMazeWidth; x++) {
for (int y = 0; y < m_nMazeHeight; y++) {
if (m_maze[y * m_nMazeWidth + x] & CELL_VISITED) {
Draw(x, y, PIXEL_SOLID, FG_WHITE);
}
else {
Draw(x, y, PIXEL_SOLID, FG_BLUE);
}
}
}
return true;
}
};
int main()
{
//create maze and run
mazeGenerator game;
game.ConstructConsole(160, 100, 8, 8);
game.Start();
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
这是 lonecoders 引擎 https://github.com/OneLoneCoder/videos/blob/master/olcConsoleGameEngine.h
我会发布引擎本身的代码,但我最终会得到超过 30000 个字符,对不起!
解决方案
我认为问题在于您在派生类中编写的函数onUserUpdate
应该是OnUserUpdate
- 大写O
。另外,override
在函数实现前加上关键字。改变
virtual bool onUserUpdate(float fElppasedTime) {
^^^^^^^
进入这个
bool onUserUpdate(float fElppasedTime) override {
^^^^^^^^
对于所有被覆盖的函数 - 你会看到错误。