Unreachable base class

I am writing a simple game in SDL. I have a class hierarchy that I create for any sprite that I use in the game. The base class is Sprite, which contains data abstractions for the collision and sprite window. Immediately below are two abstract classes: Character and MapObject.

I am currently implementing the Player class, which is derived from Character (Enemy and NPC will also be derived from the abstract Character class).

In any case, I hope this makes sense. My problem is this:

When I try to use the player class in the engine, I cannot access any of the Sprite features.

I get the following error:

'Sprite' is not an accessible base of 'Player' 

Here are the header files:

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

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

I get the first error in my engine when I try to call the configuration functions from Sprite in the player, the first of which:

 player.setLoc(levels[0].startX(), levels[0].startY()); 

Any help is appreciated.

[SOLVED] EDIT: alternative comment solution: the character class did not inherit anything from the Sprite class, so technically it did not need to be derived from it. Instead of inheriting Character from Sprite, I had a Player inheriting from BOTH Sprite and Character, and it also worked. I am not sure if this is the best design.

+6
source share
3 answers

I think you need to change

 class Player : protected Character{ 

to

 class Player : public Character{ 

In this way, you can access the Character and Sprite functions of the Player object from a Player instance created anywhere in your program.

If a Player can do anything that Character can do, public inheritance makes sense. There is no reason to hide something in Player that is freely available in Character or Sprite objects.

+11
source

Always use public inheritance to represent the is-a relationship. Player seems to be Character , so inheritance should be public , not protected .

Inheritance

protected and private inherit more as a "has-a" (and in most cases, a member subobject is an easier way to handle this relationship).

+1
source

I would say that since Character is a protected base class, it restricts access to Sprite .

0
source

Source: https://habr.com/ru/post/922656/


All Articles