类无法访问的指针

时间:2016-10-12 12:13:18

标签: c++

你好需要你的帮助才能解决这个问题,或者只是解释一下我的指针无法访问的原因。

PlayerSprite.h

#pragma once
#include "LoadPlayerRes.h"
#include "Keyboard.h"
#include "PlayerState.h"

class PlayerSprite :public LoadPlayerRes{
public:
    PlayerSprite(float x, float y,float speed)
        :
        x(x),
        y(y),
        speed(speed)
    {
        pCurrentState = new StateStand(&Stand);
    }
    void Controls(Keyboard& kbd) {
        speed = 0;
        if (speed == 0) {
            delete pCurrentState;
            pCurrentState = new StateStand(&Stand);
        }
    }
    void Draw(Graphics& gfx) {
        pCurrentState->pSprite->Draw(x, y, gfx);// pSprite inaccessible 
    }
private:
    float x, y,speed;
    PlayerState* pCurrentState;
};

PlayerState.h

#pragma once
#include "SurfaceAnimation.h"
class PlayerState {
public:
    PlayerState(SurfaceAnimation* pSprite)
        :
        pSprite(pSprite)
    {}
protected:
    SurfaceAnimation* pSprite;
};

class StateStand :public PlayerState {
public:
    StateStand(SurfaceAnimation* pSprite)
        :
        PlayerState(pSprite)
    {}
};

所以我试图创建一个播放器状态机,PlayerState类的工作是指向右SurfaceAnimation对象并基于指针我将在PlayerSprite类中绘制一个播放器,但由于某种原因pSprite是不可访问的。

1 个答案:

答案 0 :(得分:8)

由于您将其设为perparesegue,因此无法访问。

因此,只有protected中的函数(或继承PlayerState的类中的函数)才能看到它。

您正尝试从PlayerState中的某个功能访问它。

相关问题