C++ 错误:抽象类类型FancyPlayer cpp的新表达式无效
我在main.cpp上有以下代码:C++ 错误:抽象类类型FancyPlayer cpp的新表达式无效,c++,dev-c++,C++,Dev C++,我在main.cpp上有以下代码: #include <iostream> #include <limits> #include <conio.h> #include "Ghost/SmartGhost/SmartGhost.h" #include "Player/Player.h" #include "PlayerInfo/PlayerInfo.h" #include "GameBoard/GameBoard
#include <iostream>
#include <limits>
#include <conio.h>
#include "Ghost/SmartGhost/SmartGhost.h"
#include "Player/Player.h"
#include "PlayerInfo/PlayerInfo.h"
#include "GameBoard/GameBoard.h"
#include "ManualPlayer/ManualPlayer.h"
#include <vector>
using namespace std;
class Position {
public:
int x;
int y;
Position();
Position(int xo,int yo) {x=xo; y=yo;} };
class FancyPlayer : public Player
{
private: vector<Position> visited; //vector of visited nodes int size[2]; //width and height of maze Position start; vector <Position> path; //last visited node(backtrack) public:
void init(int width,int height,int x,int y) {
size[0]=width;
size[1]=height;
start.x=x; //starting position
start.y=y; visited.push_back(Position(start.x,start.y)); path.push_back(Position(start.x,start.y)); }
bool isValid(char ** ViewAround,int x,int y) {
return (ViewAround[x][y]!='@' && ViewAround[x][y]!='*'); }
bool isvisited(int x,int y){
bool f=false;
for(int i=0;i<visited.size();i++) {
if (visited[i].x==x && visited[i].y==y)
f=true;
} return f; }
int getMove(char** ViewAround) {
if (isValid(ViewAround,1,2) && !isvisited(start.x-1,start.y))
{
visited.push_back(Position(start.x-1,start.y));
path.push_back(Position(start.x-1,start.y));
start.x--;
return 0; }
else if (isValid(ViewAround,3,2) && !isvisited(start.x+1,start.y))
{
visited.push_back(Position(start.x+1,start.y));
path.push_back(Position(start.x+1,start.y));
start.x++;
return 1; } else if (isValid(ViewAround,2,3) && !isvisited(start.x,start.y+1))
{
visited.push_back(Position(start.x,start.y+1));
path.push_back(Position(start.x,start.y+1));
start.y++;
return 2; } else if (isValid(ViewAround,2,1) && !isvisited(start.x,start.y-1))
{
visited.push_back(Position(start.x,start.y-1));
path.push_back(Position(start.x,start.y-1));
start.y--;
return 3; } else
{
if (path[path.size()-1].x<start.x){
path.pop_back();
start.x++;
return 1;
}
if (path[path.size()-1].x>start.x){
path.pop_back();
start.x--;
return 0;
}
if (path[path.size()-1].y<start.y){
path.pop_back();
start.y++;
return 2; }
if (path[path.size()-1].y>start.y){
path.pop_back();
start.y--;
return 3;
}
}
}
std::string getName() { return std::string("mplampla"); }
std::string getId() { return std::string("cs141065"); }
};
int main() {
std::vector<ObjectInfo *> PlayersVector; //vector with players engaged
PlayersVector.push_back(new PlayerInfo(*new FancyPlayer(), 'A')); //Erase the comments to play with keyboard
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'B')); //Fool Standby player
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'C')); //Fool Standby player
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'D')); //Fool Standby player
GameBoard *MainGameObject; //Main Game Object
InfoBoard *ptrToInfoBoard = new InfoBoard();
for (int j = 0; j < PlayersVector.size(); ++j) {
ptrToInfoBoard->addPlayer(*static_cast<PlayerInfo *>(PlayersVector[j]));
}
std::ostringstream he;
std::vector<std::string>*ptr = GameBoard::getMapFileNames(DEVCPP_PATH);
for (unsigned int i = 0; i < ptr->size(); ++i) {
he<<DEVCPP_PATH<<ptr->operator[](i);
for (int j = 0; j < EATCH_MAP_PLAY_TIMES; ++j) {
MainGameObject=new GameBoard(true,he.str().c_str(),PlayersVector,EXETASI_MODE,ptrToInfoBoard);
MainGameObject->StartGame();
delete(MainGameObject);
getchar();
}
he.str("");
}
while(1); }
StupIdLayer.cpp:
#include <cstdlib>
#include "StupidPlayer.h"
#include <fstream>
int StupidPlayer::getMove(const char **ViewAround) {
std::ofstream he("inner.txt");
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
he<<ViewAround[i][j];
}
he<<'\n';
}
return STAND;
}
void StupidPlayer::init(int width, int height, int x, int y) {
}
std::string StupidPlayer::getName() {
return std::string("StupidPlayer");
}
std::string StupidPlayer::getId() {
return std::string("cs161119");
}
#包括
#包括“StupidPlayer.h”
#包括
int StupidPlayer::getMove(常量字符**ViewAround){
std::流he(“inner.txt”);
对于(int i=0;i<5;++i){
对于(int j=0;j<5;++j){
嘿,这个:newplayerInfo(*new FancyPlayer(),…)无论怎样,
都会导致内存泄漏。我不知道是谁/什么让你这么做的,但除非PlayerInfo
的神秘构造函数将第一个参数存储为引用,然后采取罕见的、不寻常的步骤,最终通过delete&ref;
删除它,否则该代码最坏是错误的,最好是糟糕的。Wh@WhozCraig说他更喜欢使用智能指针指向新的。与你的问题相关的int-getMove(const-char**ViewAround)
是在基础的虚拟纯中,但是int-getMove(char**ViewAround)
在派生的FancyPlayer
中实现。它们不匹配,因此…抽象基类。检测这是发明覆盖
的原因之一。在StupidPlayer
类中是正确的。这就是我必须连接“FancyPlayer类”的方式使用main…那么有什么建议如何找到这种格式的解决方案吗?main.cpp[Error]`int FancyPlayer::getMove(char**)`标记了override,但没有覆盖`Fyi,这是:new PlayerInfo(*new FancyPlayer(),…)无论怎样,
都会导致内存泄漏。我不知道是谁/什么让你这么做的,但除非PlayerInfo
的神秘构造函数将第一个参数存储为引用,然后采取罕见的、不寻常的步骤,最终通过delete&ref;
删除它,否则该代码最坏是错误的,最好是糟糕的。Wh@WhozCraig说他更喜欢使用智能指针指向新的。与你的问题相关的int-getMove(const-char**ViewAround)
是在基础的虚拟纯中,但是int-getMove(char**ViewAround)
在派生的FancyPlayer
中实现。它们不匹配,因此…抽象基类。检测这是发明覆盖
的原因之一。在StupidPlayer
类中是正确的。这就是我必须连接“FancyPlayer类”的方式使用main…那么有什么建议如何找到这种格式的解决方案吗?main.cpp[Error]`'int FancyPlayer::getMove(char**)`标记了覆盖,但没有覆盖`
#ifndef GHOST_STUPIDPLAYER_H
#define GHOST_STUPIDPLAYER_H
#include "../Player/Player.h"
#include "../Move.h"
class StupidPlayer: public Player {
int getMove(const char **ViewAround);
void init(int width,int height,int x,int y);
std::string getName();
std::string getId();
};
#endif //GHOST_STUPIDPLAYER_H
#include <cstdlib>
#include "StupidPlayer.h"
#include <fstream>
int StupidPlayer::getMove(const char **ViewAround) {
std::ofstream he("inner.txt");
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
he<<ViewAround[i][j];
}
he<<'\n';
}
return STAND;
}
void StupidPlayer::init(int width, int height, int x, int y) {
}
std::string StupidPlayer::getName() {
return std::string("StupidPlayer");
}
std::string StupidPlayer::getId() {
return std::string("cs161119");
}