继承的精灵不可绘制

时间:2017-08-18 22:32:04

标签: c++ class inheritance sfml

在SFML中我想要一个sprite但是有其他函数和变量所以我决定创建一个继承sprite类的类,如下所示:
1. Player.hpp

#pragma once

#include <stdio.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

class Player : sf::Sprite{
    public:
        void setup();
        void input();
    private:
        int direction;
        void moveRight();
        void moveLeft();
        sf::Texture textures[8];
};

2(WIP)。 Player.cpp

void Player::setup(){
    direction = 0;
    textures[0].loadFromFile("images/player_right_still.png");
    textures[1].loadFromFile("images/player_right_jump.png");
    textures[2].loadFromFile("images/player_right_walk1.png");
    textures[3].loadFromFile("images/player_right_walk2.png");
    textures[4].loadFromFile("images/player_left_still.png");
    textures[5].loadFromFile("images/player_left_jump.png");
    textures[6].loadFromFile("images/player_left_walk1.png");
    setTexture(textures[0]);
}

void Player::input(){
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
        moveRight();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
        moveLeft();
    }
}

void Player::moveRight(){

}

void Player::moveLeft(){

}
  1. game.cpp(图片因为SO对我来说很奇怪) game.cpp
  2. 但是当我编译它时,我收到了这个错误:

    game.cpp: In function ‘int main()’:
    game.cpp:23:27: error: ‘sf::Drawable’ is an inaccessible base of ‘Player’
             window.draw(player);
    

1 个答案:

答案 0 :(得分:2)

class Player : sf::Sprite表示继承是私有的,即使用Player类的实例的代码无法将其强制转换为Sprite或从{{1}继承的访问方法} .class。您应该将继承更改为Sprite

public