2014-04-28 132 views
0

爲什麼我無法實例化?爲什麼玩家是抽象類? 我是否僅在基類中聲明純虛函數?錯誤錯誤C2259:'玩家':無法實例化抽象類

class Object 
{ 
protected: 
public: 
    virtual void DrawOnScreen() = 0; 
}; 

class Creature : public Object 
{ 
public: 
    virtual void DrawOnScreen()=0; 
    virtual void Eat(Object*)=0; 
    virtual void move(Direction)=0; 
}; 

class Player : public Creature 
{ 
    void Player::DrawOnScreen() 
    { 
     cout << "position= (" << get_x() << "," << get_y() << ")" << endl << "player's score " << points << endl; 
     if (get_isKilled()) cout << "It is Killed " << endl; 
     else cout << "It is Alive " << endl; 
    } 

    void Player::eat(Object* p) 
    { 
     Point* chk; 
     chk = dynamic_cast<Point*>(p); 
     if ((chk != 0) && (get_x() == chk->get_x()) && (get_y() == chk->get_y()) && (chk - > get_isExist())) 
     { 
      points = points + chk->get_weight(); 
      chk->set_isExist(false); 
     } 
    } 

    void Player::move(Direction d) 
    { 
     if (d == UP) 
     { 
      y = y + 2; 
     } 
     if (d == DOWN) 
     { 
      y = y - 2; 
     } 
     if (d == RIGHT) 
     { 
      x = x + 2; 
     } 
     if (d == LEFT) 
     { 
      x = x - 2; 
     } 
    } 
} 
+2

我敢肯定,錯誤消息列出了確實純粹的功能,你沒有覆蓋RESP。沒有執行。 – Deduplicator

+0

C++區分大小寫。寫吃飯和大寫搬家 – slfan

+0

@slfan:移動是可編譯的,儘管不一致。它不會與std :: move衝突。 – lpapp

回答

2

的問題是,你不能實例化一個類與這裏發生了因繼承而誤認爲至少覆蓋一個純虛方法。

這是一個錯字:

void Player::eat(Object*p) 

你真正的意思是「吃」的資本,因此,你實際上並沒有覆蓋。你應該寫這樣的:

void Player::Eat(Object*p) 

最重要的是,你真的應該刪除Player::作用域爲類中的方法,或將像外面的方法。

另外,請一個偉大的使用C++ 11 override關鍵字,以避免這樣的問題,所以這樣的事情會給你一個編譯錯誤:

void Eat(Object *p) override; 
相關問題