简体   繁体   中英

How can I cast a pointer to call a derived class function/method? C++

In my rogue-like game based off of netback, I have a base Entity with a derived class called Creature. I also have another derived class of Creature called Player.

Since Player is basically a Creature but with added/modified methods, my attack method (for both) takes 2 pointers to Creature objects. One for the Creature being attacked and one for the Player.

In the player version of the virtual method, attack, I need to set the player's XP and Score member variables to reflect the battle. I keep getting errors saying:

Player.cpp:34:11: error: ‘class Creature’ has no member named ‘setScore’
Player.cpp:34:29: error: ‘class Creature’ has no member named ‘getScore’

Which I'm assuming is because it's of Creature type.. but I can't get it to cast so that I can actually utilize the methods on the player.

I also noticed that I have "setXp" and "getXp" defined in Creature. But Creature's don't have xp.. Could/should I just add the score methods to creature and define them in Player?

How can I get it to do this? Thanks! Here's some of the code. Let me know if you need more:

Player.h

#ifndef _Player_included_
#define _Player_included_

#include "Creature.h"
#include "Weapon.h"
#include "Armor.h"

class Player : public Creature {

public:
    Player(void);
    virtual ~Player(void);

    void dumpStats();
    virtual bool move(char dir, DungeonLevel & dl, std::mt19937 & randomGen$
    virtual bool canAttack(DungeonLevel & dl);
    int getHit(std::mt19937 & randomGen);
    bool fightOrFlight(DungeonLevel & dl, std::mt19937 & randomGen);
    virtual void attack(Creature * monster, Creature * player, std::mt19937$

    void generateHP();
    void addXp(int xpToAdd);
    void addScore(int xpToMultiply); //score is just 2x the xp.
    virtual int getScore();
    virtual void setScore(int scoreToSet);
    virtual int getXp();
    virtual void setXp(int xpToSet);

    void setXPToLevel(int xpToLevelToSet);
    int getXPToLevel();
private:
    //Creature provides level, HP, and maxHP
    int score;
    int xp;
    int xpToLevel;
    Weapon * playerWeapon;
    Armor * playerArmor;
};

#endif

Attack method for Player.cpp

void Player::attack(Creature * monster, Creature * player, std::mt19937 & randomGen, Du$
    int monsterHit = monster->getHit(randomGen);
    int playerHit = getHit(randomGen);

    player = static_cast<Player*>(player);

    if ((monster->getHP() - playerHit) <= 0){
            playerHit = monster->getHP();
            player->addXp(playerHit);
            player->setScore((player->getScore()) + (2 * playerHit)));
            cout << "Monster name: " << monster->getName() << endl;
            cout << "monster killed: " << monster << endl;
            monster->removeMonster(dl);
    }
    else if (monster != NULL){
            cout << "Monsters Hit: " << monsterHit << endl;
            player->setHP((player->getHP()) - monsterHit);
            player->addXp(playerHit);
            player->setScore((player->getScore()) + (2 * playerHit)));
            monster->setHP((monster->getHP() - playerHit));
            cout << "Your HP: [" << player->getHP() << "]/[" << player->getMaxHP() $
            cout << "Monsters HP: [" << monster->getHP() << "]/[" << monster->getMa$
    }
    else if ((player->getHP() - monsterHit) <= 0){
            monsterHit = player->getHP();
            //game over
    }

    cout << "You hit: " << playerHit << endl;
}

Creature.h

#ifndef _Creature_included_
#define _Creature_included_

#include "Entity.h"
#include "DungeonLevel.h"

#include <random>

class Creature : public Entity {

public:
    Creature(void);
    virtual ~Creature(void);

    virtual void dumpObject();
    virtual void dumpObjectData();
    virtual void writeFragment(std::ostream & output);
    virtual void writeDataAsFragment(std::ostream & output);
    virtual void setElementData(std::string elementName, std::string elementValue);

    virtual bool move( DungeonLevel & dl, Creature & player, std::mt19937 & randomGen);
    virtual void attack(Creature * monster, Creature & player, std::mt19937 & randomGen, DungeonLevel & dl);
    virtual int getHit(std::mt19937 & randomGen);

    virtual bool canAttack();
    virtual void removeMonster(DungeonLevel & dl);

    virtual void setXLoc(int xToSet);
    virtual int getXLoc();
    virtual void setYLoc(int yToSet);
    virtual int getYLoc();

    virtual void setXp(int xpToSet);
    virtual int getXp();
    virtual void addXp(int xpToAdd);

    virtual int getLevel();
    virtual void setLevel(int levelToSet);
    virtual int getHP();
    virtual void setHP(int HPToSet);
    virtual int getMaxHP();
    virtual void setMaxHP(int maxHPToSet);

private:
    int xLoc;
    int yLoc;
    int level;
    int HP;
};

#endif

Attack method in Creature.cpp

void Creature::attack(Creature * monster, Creature & player, std::mt19937 & randomGen,  DungeonLevel & dl){
    int monsterHit = monster->getHit(randomGen);
    int playerHit = player.getHit(randomGen);

    if ((monster->getHP() - playerHit) <= 0){
            playerHit = monster->getHP();
            cout << "Monster name: " << monster->getName() << endl;
            this->removeMonster(dl);
            cout << "back to creature attack with monster removed.."<<endl; 
            cout << "delete monster here." << endl;
    }
    else if ((player.getHP() - monsterHit) <= 0){
            cout << "You died. Game Over." << endl;
            //make a function to end the game
    }
}

The line

player = static_cast<Player*>(player);

actually does nothing. You still have the same player variable of the same Creature* type (because it is declared with this type). Instead, you could write

Player* player_ref = reinterpret_cast<Player*>(player); and then use player_ref -> setScore(); .

A polymorphic solution would be neater though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM