2013-10-14 114 views
0

我製作了一個簡單的基於文本的格鬥遊戲,並且我有很多麻煩讓我的子類工作。不能讓子類正常工作

了許多錯誤的

即時得到最持續性是「行定義我們的‘小矮人’不符的任何聲明,‘矮人’

#include <iostream> 
using namespace std; 

class Poke{ 
protected: 
    string race; 
    int health, damage, shield; 
public: 
    Poke(); 
    Poke(int health, int damage, int shield); 
    virtual int attack(Poke*); 
    virtual int defend(Poke*); 
    virtual int getHealth(); 
}; 

這是不同的比賽之一sublcasses,有2個不同級別的攻擊/健康/屏蔽

// Dwarf: High health, Low attack, High defense 
class Dwarf: public Poke { 
public: 
    string race = "Dwarf"; 
    int attack(Poke*); 
    int defend(Poke*); 
    int getHealth(); 

}; 

的.cpp V

//DWARF 
Dwarf::Dwarf(int health, int damage, int shield) { 
    this->health = 100; 
    this->damage = 50; 
    this->shield = 75; 
}; 

//attack 
int Poke:: attack(Poke*){ 
    if (shield > (damage + rand() % 75)){ 
     cout << "Direct Hit! you did" << (health - damage) << "points of damage"; 
    } 
    else {std::cout << "MISS!"<<; 
    } 
    return 0; 
}; 

int Poke:: attack(Poke*){ 
    Enemy this->damage ; 
}; 

我使用的是球員類的人打,將使用「戳」

class Player{ 
    int wins, defeats, currentHealth; 
    string name; 
    Poke race; 
    bool subscribed; 
public: 
    Player(int wins, int defeats, int currentHealth); 
    int addWins(); 
    int addDefeats(); 
    int getWins(); 
    int getDefeats(); 
    int getHealth(); 


}; 

的.cpp V

//getHealth 
int Player::getHealth(){ 
    return this->currentHealth; 
}; 

,並和計算機對手「敵」類遊戲:

class Enemy{ 
    int eHealth; 
    Poke eRace; 
public: 
    Enemy (int eHealth, Poke eRace); 
    int getEHealth; 
}; 

的.cpp V

int Enemy:: getEHealth(){ 
    return this->eHealth; 
}; 

任何幫助將不勝感激!

+1

您還沒有指定什麼錯誤。你能告訴我們這個代碼的行爲與你的期望有什麼不同嗎? –

+0

即時通訊錯誤消息「我們的行定義」矮人「不符合」矮人「的任何聲明 – thesowismine

回答

0

構造函數不被繼承。您必須聲明符合您定義的Dwarf構造函數。

我想你也有麻煩,這樣的:

string race = "Dwarf"; 

您不能初始化類成員的方式。它必須在構造函數中初始化。

編輯:

你似乎不明白我的意思是什麼聲明。更改Dwarf類的聲明看起來是這樣的:

// Dwarf: High health, Low attack, High defense 
class Dwarf: public Poke { 
public: 
    string race; 

    Dwarf(int health, int damage, int shield); // <-- constructor declaration 
    int attack(Poke*); 
    int defend(Poke*); 
    int getHealth(); 

}; 

編輯2:

Dwarf構造也應該調用Poke構造,像這樣:

Dwarf::Dwarf(int health, int damage, int shield) : 
    Poke(health, damage, shield), 
    race("Dwarf") 
{ 
    // Nothing needed here. 
}; 
+0

我試過把矮人::矮人:戳();這是我的教授指出,相信會工作,但它不是 – thesowismine

+0

這是定義,而不是聲明,你需要在你的類中聲明它,這是你的錯誤信息告訴你的 –

+0

我試過把Dwarf :: Dwarf:Poke();它並不像其他冒號,申報的正確方法是什麼? – thesowismine