C++ 继承错误

标签 c++ class inheritance

这是触发错误的代码 (Player.cpp):

#include "Library.h"

Player::Player(){
    //generate player stats
    str = rand()%6+1+1;
    inte = rand()%6+1;
    c = (rand()%6+1)+floor(str/3);
    wis = rand()%6+1+floor(inte/4);
    ref = rand()%6+1+floor(wis/4);
    i = floor(ref/3);
    hp = floor((str+(wis/3)+(ref/2)));
    xp = 0;
}

//printStats (constant Player player reference)
//prints player's stats
void Player::printStats() const{
    cout << "\nSTR: " << str << endl;
    cout << "INTE: " << inte << endl;
    cout << "C: " << c << endl;
    cout << "WIS: " << wis << endl;
    cout << "REF: " << ref << endl;
    cout << "I: " << i << endl;
    cout << "HP: " << hp << endl;
    cout << "XP: " << xp << endl;
    cout << "Gold: " << gold << endl;
    cout << "Level: " << lvl << endl << endl;
}

int Player::giveOptions(int amount,string op1, string op2, string op3, string op4, string op5){
    cout << "Type the number then press the enter key to choose or type 'help' for extra commands." << endl;
    for(int i=1;i<=amount;i++){
        string s;
        switch(i){
        case 1:
            s = op1;
            break;
        case 2:
            s = op2;
            break;
        case 3:
            s = op3;
            break;
        case 4:
            s = op4;
            break;
        case 5:
            s = op5;
            break;
        }
        cout << i << ". " << s << endl;
    }
    while(true){
        string s;
        cin >> s;
        if (s == "1")
            return 1;
        else if (s == "2")
            return 2;
        else if (s == "3")
            return 3;
        else if (s == "4")
            return 4;
        else if (s == "5")
            return 5;
        else{
            if (s == "stats")
                printStats();
            else if (s == "help"){
                cout << "Type the number that is next to the option you wish to choose then press the enter key, or 'stats' to print all of your stats." << endl;
                cout << "E.G:\n1. Town\nI want to go to the town\n1" << endl;
            }
            else
                cout << "Command not recognised. If you're confused, type 'help'." << endl;
        }

    }
}

(下面是原始问题)

我的 C++ 相当基础,我不确定为什么会产生错误。在 Player.cpp 中,我认为继承的所有 Entity 成员都会产生错误,“x 不是 Player 的成员”。我唯一的想法是我错误地使用了继承。

实体.h:

#include "Library.h"

using namespace std;
class Entity {
public:
    void printStats() const;
protected:
    //player stats
    std::string name;
    double str;     //strength
    double wis;     //wisdom
    double ref;     //reflex
    double hp;      //health points
    double i;       //initiative
    double inte;    //intelligence
    double c;       //courage
    int gold;       //gold
    int xp;         //experience
    int ap;         //armour points
    int wd;         //weapon damage
    int lvl;        //level
    int sp;         //skill points
};

播放器.h

#include "Library.h"

using namespace std;

class Player: public Entity{
public:
    Player();
    int giveOptions(int amount, string op1, string op2, string op3, string op4, string op5);
};

最佳答案

void Player::printStats() const

应该是,根据你的标题:

void Entity::printStats() const

在包含文件中,执行以下操作之一,以最适合您的代码为准:

1.

  • Player.h 必须包含 Entity.h
  • Library.h 应该包括Player.hEntity.h
  • 如果确实需要,
  • Player.h 和/或 Entity.h 可以包含 Library.h

或 2.

  • Player.h 必须包含 Entity.h,但 Library.h
  • Entity.h 必须包含Library.h
  • Library.h 可以包含 Player.h 和/或 Entity.h

这避免了您当前拥有的循环依赖 - 这会导致 PlayerEntity 之前被定义并给出 base class undefined 错误。

关于C++ 继承错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395597/

相关文章:

基类中的 C++ 方法

c++ - 为什么 C++ 允许永远不能使用的默认模板参数?

c++ - 如何在 C++ 中为指针 'this' 赋值

objective-c - cocoa /Objective-C : Access a (Boolean) variable in different Classes

c# - 如何根据派生类字段过滤基类集合?

java - 重写(非)静态类中的私有(private)方法

c++ - dlib 在屏幕上显示值

c++ - 数组与 vector : Introductory Similarities and Differences

c# - 如何初始化一个类?

C++ 类 : using a private array in a public function?