开发者

C++ Inheritance error

Here's the code that triggers the error (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;
    开发者_如何学Python        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;
        }

    }
}

(Original question below)

I'm fairly basic in C++, and I'm not sure why this is producing an error. In Player.cpp, all members of Entity that I thought were inherited produce the error, "x is not a member of Player". My only thought is that I'm using inheritance wrong.

Entity.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
};

Player.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

Should be, according to your headers:

void Entity::printStats() const

On the includes, do one of these, whichever suits your code best:

1.

  • Player.h must include Entity.h
  • Library.h should not include Player.h or Entity.h
  • Player.h and/or Entity.h can include Library.h if really needed.

or 2.

  • Player.h must include Entity.h, but not Library.h
  • Entity.h must not include Library.h
  • Library.h can include Player.h and/or Entity.h

This avoids the cyclic dependencies you currently have - which leads to Player being defined before Entity and giving the base class undefined error.


As the compiler compliains - neither the class Entity nor the Player has a member variable called x. You need to include "Entity.h" in the Player header file, since in the current translation unit compiler doesn't know what Player is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜