Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 不可访问基类_C++_Class_Inheritance_Polymorphism - Fatal编程技术网

C++ 不可访问基类

C++ 不可访问基类,c++,class,inheritance,polymorphism,C++,Class,Inheritance,Polymorphism,我正在用SDL写一个简单的游戏。我为游戏中使用的任何精灵建立了一个等级继承权。基类是Sprite,它包含冲突框和spritesheet的数据抽象。正下方是两个抽象类,Character和MapObject 我目前正在实现从角色派生的玩家类(敌人和NPC也将从抽象角色类派生) 无论如何,希望这是有道理的。我的问题是: 当我尝试在引擎中使用player类时,我无法访问任何Sprite函数 我得到以下错误: 'Sprite' is not an accessible base of 'Player'

我正在用SDL写一个简单的游戏。我为游戏中使用的任何精灵建立了一个等级继承权。基类是Sprite,它包含冲突框和spritesheet的数据抽象。正下方是两个抽象类,Character和MapObject

我目前正在实现从角色派生的玩家类(敌人和NPC也将从抽象角色类派生)

无论如何,希望这是有道理的。我的问题是:

当我尝试在引擎中使用player类时,我无法访问任何Sprite函数

我得到以下错误:

'Sprite' is not an accessible base of 'Player'
以下是头文件:

Sprite.h:

class Sprite{
public:
    virtual ~Sprite() = 0;  //I want this to be an abstract class and yes this is defined in the cpp

    //Initialization functions - MUST be called before anything else in the class can be used.
    void setupCollider(int xo, int yo, int ho, int wo);
    void setupSheet(SDL_Surface* dest, std::string file, int numClips, int ac, int _res, int sr);

    //SpriteSheet functions
    void setActiveClip(int a);
    int getActiveClip() const;
    void draw() const;
    int getResolution() const;
    SDL_Surface* getDest();


    //Collider functions
    void updateCollider();
    SDL_Rect box() const;
    bool collision(SDL_Rect other) const;

    //Access and Modify coordinates of the sprite
    void setLoc(int x, int y) { _x = x; _y = y; }
    int getX() const { return _x; }
    int getY() const { return _y; }

private:
    struct Collider{
        SDL_Rect  _box;
        int       x_offset,
                  y_offset;
    };

    struct SpriteSheet{
        SDL_Surface*     destination;
        SDL_Surface*    sheet;
        SDL_Rect*       clips;
        int             _numClips;
        int             active;
        int             res;
        int             sheetrows;
    };


    Collider     collisionAttributes;
    SpriteSheet  spriteSheetAttributes;
    int          _x, _y;

};
字符h:

class Character : public Sprite{
public:
    virtual void move(Direction direction_input, const TileMap& currentlevel) = 0;
    virtual void animate() = 0;

    virtual void setAttributes( int h, int sp, int ad, int d, int m, int md, int l, std::string n){
        hearts = h; speed = sp; attackdamage = ad;
        defense = d; magicdamage = m; magicdefense = md;
        level = l; name = n;
    }

    bool isDead() const{
        return hearts == 0;
    }

    void heal(int heartsup){
        hearts += heartsup;
    }

    void dealDamage(int heartsDown){
        hearts -= heartsDown;
    }


protected:

    int hearts;
    int speed;
    int attackdamage;
    int defense;
    int magicdamage;
    int magicdefense;
    int level;
    std::string name;

};
Player.h:

//Not fully finished yet, but should work for the purposes of this question
class Player : protected Character{
public:
    ~Player(){
        if(heart) SDL_FreeSurface(heart);
    }
    static const int HEART_WIDTH;

    void move(Direction direction_input, const TileMap& currentlevel);
    void animate();

    void updateCamera(TileMap& currLevel);


 private:

    SDL_Surface* heart;

    enum ClipFace
    {
        UP1,
        UP2,
        DOWN1,
        DOWN2,
        LEFT1,
        LEFT2,
        RIGHT1,
        RIGHT2
    };

    static const std::string HEART;
    static const int HEART_RES;

};
当我试图从Sprite in player调用设置函数时,我的引擎中出现了第一个错误,第一个错误是:

player.setLoc(levels[0].startX(), levels[0].startY());
非常感谢您的帮助


[已解决]编辑:注释的替代解决方案:character类没有从Sprite类继承任何内容,因此从技术上讲,它不必从Sprite类派生。我没有让角色从精灵继承,而是让玩家同时从精灵和角色继承,这同样有效。我不确定什么设计更好。

我敢打赌,既然
字符
是受保护的基类,它限制了对
精灵的访问,我想你需要改变

class Player : protected Character{

这样,您就可以从程序中任意创建的
Player
实例访问
Character
Sprite
Player
对象上的函数


如果
玩家
应该能够做
角色
可以做的任何事情,
公共
继承是有意义的。没有理由在
Player
中隐藏可以在
Character
Sprite
对象中自由访问的内容。

始终使用
public
继承来表示“is-a”关系。听起来像是
Player
是一个
字符
,所以继承应该是
公共的
,而不是
受保护的


代码>受保护的继承和<代码>私有< /COD>继承更像“HAS-A”(并且在大多数情况下,成员子对象是处理这种关系的一种更简单的方法)。

您明确地限制了访问,但期望它可以自由访问吗?您可能需要考虑制作<代码> SpRITE < /代码>成员。(可能为可选/可为空)关于
字符,而不是在那里使用继承关系。我认为让它受保护会让它有更多的访问权限。因此,现在它不再是公共的,而是公共的和受保护的。似乎我在这里缺乏理解。你能详细说明一下吗?@Slims不,不完全是!这个答案对private/prot有很好的解释ected/public:如果您在阅读后仍然感到困惑,请告诉我,我将尝试详细说明!
class Player : public Character{