gpt4 book ai didi

c++ - 类不可访问指针

转载 作者:太空狗 更新时间:2023-10-29 23:49:33 24 4
gpt4 key购买 nike

您好,需要您的帮助来解决这个问题,或者只是解释一下为什么我的指针无法访问。

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

播放器状态.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 无法访问。

最佳答案

它无法访问,因为您将其设置为protected

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

您正在尝试从 PlayerSprite 中的函数访问它。

关于c++ - 类不可访问指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39998328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com