错误无效的抽象类类型FancyPlayer cpp的new-expression

时间:2017-06-10 02:11:13

标签: 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.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); }

Player.h代码:

#ifndef GHOST_PLAYER_H
#define GHOST_PLAYER_H

#include <iostream>
#include <windows.h>
class Player{
private:
public:
    Player(){}
    virtual std::string getName()=0 ;
    virtual std::string getId()=0 ;
    virtual int getMove(const char **ViewAround)=0;
    virtual void init(int width,int height,int CurrentX,int CurrentY )=0;



};




#endif //GHOST_PLAYER_H

当我放在主要的

*new FancyPlayer(), 'A'))我收到错误,告诉&#34; [错误]抽象类类型的新表达式无效&#39; FancyPlayer&#39;&#34; ,如果我把这一行放在评论中代码正常工作......

愚蠢的游戏代码:

StupidPlayer.h:

    #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

StupidPlayer.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");
        }

我看不出愚蠢的玩家和幻想之间的任何区别......我能做些什么...为了使花哨的作品正常...这可能是一个有两个类用于幻想的问题?

0 个答案:

没有答案