こんにちは、この問題を解決するためにあなたの助けを必要とするか、私のポインタがアクセスできない理由を説明してください。クラスにアクセスできないポインタ
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にアクセスできません。
'pSprite'は' protected'ですが、なぜ関係のないクラスからアクセスできるのでしょうか? –
これは、可読性に役立ち、関数のパラメータの名前をクラスメンバ(たとえば 'pSprite(pSprite)')と異ならせると潜在的に問題を回避します。 –
@πάνταῥεp pCurrentStateはPlayerStateのポインタなので、それは私が間違っているのですか? – yahoo5000