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();
};

这是不同种族的一个子类,还有 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 ;
};

我正在为玩将使用“Poke”的游戏的人使用玩家类

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;
};

任何帮助将非常感激!!

4

1 回答 1

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.
};
于 2013-10-14T02:18:17.810 回答