我制作了一个简单的基于文本的格斗游戏,我在让子类工作方面遇到了很多麻烦。
我得到的许多错误,最持久的是“我们的行定义”矮人“与”矮人“的任何声明都不匹配
#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 ;
};
我正在为玩游戏的玩家使用“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;
};
任何帮助都会非常感激!!
答案 0 :(得分: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.
};