Я делаю простую текстовую боевую игру, и у меня возникают проблемы с работой моих подклассов.
из многих ошибок, возникающих у меня, наиболее упорным является "наше определение линии". "Гном" не соответствует объявлению "Гнома",
#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();
};
это одна подклассы разных рас, есть еще два уровня с разными уровнями атаки/здоровья/щита
// 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;
};
Любая помощь приветствуется!!
Конструкторы не наследуются. Вам нужно будет объявить конструктор 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.
};