Frage

I making a simple text based fighting game and i am having a lot of trouble getting my subclasses to work.

of the many errors im getting, the most persistant is "our of line definition "Dwarf" does not match any declaration of "Dwarf"

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

this is one sublcasses of the different races, there are 2 more with different levels of attack/health/shield

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

i am using a player class for the person playing the game that will use "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;
};

and and "enemy" class for the computer opponent:

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

.cpp V

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

any help would be much appreciated!!

War es hilfreich?

Lösung

Constructors are not inherited. You will have to declare a Dwarf constructor that matches your definition.

I think you'll also have trouble with this:

string race = "Dwarf";

You can't initialize class members that way. It will have to be initialized in a constructor.

Edit:

You don't seem to understand what I mean by a declaration. Change your Dwarf class declaration to look something like this:

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

};

Edit 2:

Your Dwarf constructor should also call the Poke constructor, like so:

Dwarf::Dwarf(int health, int damage, int shield) :
    Poke(health, damage, shield),
    race("Dwarf")
{
    // Nothing needed here.
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top