2016-10-12 10 views
2

こんにちは、この問題を解決するためにあなたの助けを必要とするか、私のポインタがアクセスできない理由を説明してください。クラスにアクセスできないポインタ

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オブジェクトを指すようにし、ポインタIに基づいて、 PlayerSpriteクラス内にプレーヤーを描画しますが、何らかの理由でpSpriteにアクセスできません。

+0

'pSprite'は' protected'ですが、なぜ関係のないクラスからアクセスできるのでしょうか? –

+0

これは、可読性に役立ち、関数のパラメータの名前をクラスメンバ(たとえば 'pSprite(pSprite)')と異ならせると潜在的に問題を回避します。 –

+0

@πάνταῥεp pCurrentStateはPlayerStateのポインタなので、それは私が間違っているのですか? – yahoo5000

答えて

8

protectedとしているためアクセスできません。

したがって、PlayerState(または、PlayerStateを継承するクラスの関数)の関数だけがそれを見ることができます。

PlayerSpriteの関数からアクセスしようとしています。

+0

pCriteStateがPlayerState * – yahoo5000

+2

@ yahoo5000であるため、pCriteStateがアクセスできるはずですが、pCurrentStateはそれにアクセスできるはずですが、 'pCurrentState'はそれにアクセスできますが、それはあなたが書いたものではありません。 –

+0

だから私はこれを解決すると思う私はそれを公開する必要があるか、またはそれにアクセスするために公開機能を作るだろうか? – yahoo5000

関連する問題