C++中的受保护变量



因此,使用一些类和继承,我创建了一个实体类,该类继承自一个从抽象 Shape 类继承的 ImageShape 类。 我还有一个从 RectShape 类继承的 Wall 类,该类继承自相同的 abstact Shape 类。

我需要解决碰撞问题,所以我调用 Wall 作为实体的友元类,并继续使用实体的受保护的 x、y、newx、newy 字段和 Wall 受保护的 x、y、宽度、高度字段创建一个函数。

编译时,编译器告诉我实体的变量是受保护的,但据我了解,如果 Wall 是实体的朋友,我应该能够使用它们;

以下是我的课程: 1. 实体类

class Shape
{
public:
virtual void show(point offset = point(), bool doStroke = false) = 0;
virtual void setColor(SDL_Color col) {color = col;}
virtual void setStrokeColor(SDL_Color col) {strokeColor = col;}
protected:
SDL_Renderer *gRenderer;
SDL_Color color;
SDL_Color strokeColor;
};
class ImageShape : public Shape
{
public:
ImageShape();
ImageShape(SDL_Renderer *&iRenderer, int iX, int iY);
bool loadFromFile(std::string path);
void show(point offset = point(), bool doStroke = false);
void free();

protected:
double x, y;
int width, height;
SDL_Texture* mTexture;
double angle;
SDL_RendererFlip flip;
};
class Entity : public ImageShape
{
friend class Wall;
public:
Entity();
Entity(SDL_Renderer *&gRenderer, int iX, int iY, double iSpeed, double iMaxSpeed, double iJumpPower);
point getCenterCoords();
void collisionWall(Wall wall);
void spin();
protected:
int newx, newy;
double dx, newdx, dy, newdy;
double speed, maxSpeed;
int jumpPower;
bool currentlyJumping;
};

2.墙面类

class RectShape : public Shape
{
public:
RectShape();
RectShape(SDL_Renderer *iRenderer, int iX, int iY, int iWidth, int iHeight, SDL_Color iColor, SDL_Color iStrokeColor = {0x00, 0x00, 0x00, 0x00});
void show(point offset = point(), bool doStroke = false);
protected:
double x, y;
int width, height;
};
class Wall : public RectShape
{
public:
Wall();
Wall(SDL_Renderer *iRenderer, int iX, int iY, int iWidth, int iHeight, SDL_Color iColor, SDL_Color iStrokeColor = {0x00, 0x00, 0x00, 0x00});
void setState(bool state) {Enabled = state;}
bool getState() {return Enabled;}
private:
bool Enabled;
};

3.这是碰撞功能

void Entity::collisionWall(Wall wall)
{
//Collision right
if(   x >= wall.x + wall.width &&
newx <= wall.x + wall.width &&
newy + height >= wall.y     &&
newy <= wall.y + wall.height  )
{
if(newdx > -6)
newdx = 0;
newdx = -newdx;
newx = wall.x + wall.width;
}
//Collision left
if(    x + width <= wall.x     &&
newx + width >= wall.x     &&
newy + height >= wall.y    &&
newy <= wall.y + wall.height )
{
if(newdx < 6)
newdx = 0;
newdx = -newdx;
newx = wall.x - width;
}
//Collision top
if(   y + height <= wall.y   &&
newy + height >= wall.y   &&
newx + width >= wall.x    &&
newx <= wall.x + wall.width )
{
newdy = 0;
newy = wall.y - height;
if(currentlyJumping)
currentlyJumping = false;
}
//Collision bot
if(    y >= wall.y + wall.height &&
newy <= wall.y + wall.height &&
newx + width >= wall.x       &&
newx <= wall.x + wall.width    )
{
newdy = -newdy / 4;
newy = wall.y + wall.height;
}
}

另外,这是我收到的部分错误:

||=== Build: Debug in The jump, again (compiler: GNU GCC Compiler) ===|
againshape.h||In member function 'void Entity::collisionWall(Wall)':|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|51|error: within this context|
againshape.h|32|error: 'int RectShape::width' is protected|
againEntity.cpp|51|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|52|error: within this context|
againshape.h|32|error: 'int RectShape::width' is protected|
againEntity.cpp|52|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|53|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|54|error: within this context|
againshape.h|32|error: 'int RectShape::height' is protected|
againEntity.cpp|54|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|59|error: within this context|
againshape.h|32|error: 'int RectShape::width' is protected|
againEntity.cpp|59|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|63|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|64|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|65|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|66|error: within this context|
againshape.h|32|error: 'int RectShape::height' is protected|
againEntity.cpp|66|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|71|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|75|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|76|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|77|error: within this context|
againshape.h|31|error: 'double RectShape::x' is protected|
againEntity.cpp|78|error: within this context|
againshape.h|32|error: 'int RectShape::width' is protected|
againEntity.cpp|78|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|81|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|87|error: within this context|
againshape.h|32|error: 'int RectShape::height' is protected|
againEntity.cpp|87|error: within this context|
againshape.h|31|error: 'double RectShape::y' is protected|
againEntity.cpp|88|error: within this context|
againshape.h|32|error: 'int RectShape::height' is protected|
againEntity.cpp|88|error: within this context|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|

啊,我明白了。 Friending Wall 意味着 Wall 可以访问 Entity 的受保护变量。 我需要和墙上的实体交朋友!

最新更新