0

我在继承函数时遇到问题,我似乎无法弄清楚它为什么会这样,并且无法在其他关于 SO 的问题中找到答案。
我正在开发一个小游戏,继承的函数负责玩家和对象之间的交互,如果玩家试图移动到已经被“障碍”的各种子类之一占据的空间,它将调用该对象的“Bool GetMove”方法,然后执行其独特的规则,如果游戏可以将玩家放置在空间上,则返回 True,否则返回 False。

这是基类标头及其 getmove 方法:

class Obstacle
{
public:
    const char* id;
    Obstacle* _properlyinitialized; //a pointer that points to the object itself, required by the teacher who gave the assignment.
    string Name;
    mutable bool Moveable;
    int x;
    int y;
    Obstacle();
    Obstacle(string Name, bool Moveable, int x, int y);
    virtual ~Obstacle();
    bool properlyInitialized();
    friend std::ostream& operator<<(std::ostream& stream, Obstacle& Obstacle);
    virtual bool getmove(const char*, std::vector<std::vector<std::vector<Obstacle> > >&);
    virtual void leavetile(std::vector<std::vector<std::vector<Obstacle> > >&);
};

bool Obstacle::getmove(const char* direction,std::vector<std::vector<std::vector<Obstacle> > >& _board){
    cout << "wrong getmove" << endl; //this method should never be called.
    return true;
};

继承的类之一及其 getmove 方法:

class Barrel: public Obstacle
{
public:
    Barrel():Obstacle(){};
    Barrel(string Name, bool Moveable, int x, int y);
    ~Barrel(){};
    bool getmove(const char*, std::vector<std::vector<std::vector<Obstacle> > >&);
    void leavetile(std::vector<std::vector<std::vector<Obstacle> > >&);
};

bool Barrel::getmove(const char* direction,std::vector<std::vector<std::vector<Obstacle> > >& _board){
    cout << "barrel getmove" << endl;
    if(strcmp(direction, "OMHOOG") == 0){ //what direction to move?
        if(_board[this->x][this->y-1][0].properlyInitialized()){ //is that object already inhabited by an object?
            if(_board[this->x][this->y-1][0].Moveable){ //can the object be moved?
                if(_board[this->x][this->y-1][0].getmove(direction, _board){//move the object
                    _board[this->x][this->y-1][0] = *this; //move the barrel
                    _board[this->x][this->y-1][0]._properlyinitialized = &_board[this->x][this->y-1][0];
                    return true; //return true
                }
                else{
                    return false; //an object is in the way, the barrel can't be moved
                }
            }
            else{
                return false; //an object is in the way, the barrel can't be moved
            }
        }
        else{
            _board[this->x][this->y-1][0] = *this; //move the barrel
            _board[this->x][this->y-1][0]._properlyinitialized = &_board[this->x][this->y-1][0];
            return true; //return true
        }
    } //This is for direction "up", i left the other directions out because they're almost equal.

该方法调用如下:

//"this" is a board object
if(this->playfield[this->_player->x][(this->_player->y)-1][0].getmove(direction, this->getBoard())){
    //do some stuff
}

我考虑过将 Obstacle 对象更改为纯虚拟对象,但我需要其他地方的虚拟“Obstacle”对象,所以这不是一个选项。

4

1 回答 1

2

的第二个参数getmove应该是

std::vector<std::vector<std::vector<Obstacle*>>>&

请注意,最里面的向量是 ofObstacle*而不是Obstacle? 这样会调用正确的虚函数,更重要的是,您不会遇到对象切片

于 2015-06-10T11:14:02.517 回答