[英]Protected variables in C++
因此,使用一些类和继承,我创建了一个继承自 ImageShape 类的实体类,该类继承自抽象 Shape 类。 我还有一个继承自 RectShape 类的 Wall 类,该类继承自同一个抽象 Shape 类。
我需要解决碰撞问题,因此我将 Wall 称为 Entity 的友元类,然后使用 Entity 的受保护 x、y、newx、newy 字段和 Wall 的受保护 x、y、宽度、高度字段继续创建函数。
编译时,编译器告诉我 Entity 的变量是受保护的,但根据我的理解,如果 Wall 是 Entity 的朋友,我应该可以使用它们;
这是我的课程: 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) ===|
again\shape.h||In member function 'void Entity::collisionWall(Wall)':|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|51|error: within this context|
again\shape.h|32|error: 'int RectShape::width' is protected|
again\Entity.cpp|51|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|52|error: within this context|
again\shape.h|32|error: 'int RectShape::width' is protected|
again\Entity.cpp|52|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|53|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|54|error: within this context|
again\shape.h|32|error: 'int RectShape::height' is protected|
again\Entity.cpp|54|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|59|error: within this context|
again\shape.h|32|error: 'int RectShape::width' is protected|
again\Entity.cpp|59|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|63|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|64|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|65|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|66|error: within this context|
again\shape.h|32|error: 'int RectShape::height' is protected|
again\Entity.cpp|66|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|71|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|75|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|76|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|77|error: within this context|
again\shape.h|31|error: 'double RectShape::x' is protected|
again\Entity.cpp|78|error: within this context|
again\shape.h|32|error: 'int RectShape::width' is protected|
again\Entity.cpp|78|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|81|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|87|error: within this context|
again\shape.h|32|error: 'int RectShape::height' is protected|
again\Entity.cpp|87|error: within this context|
again\shape.h|31|error: 'double RectShape::y' is protected|
again\Entity.cpp|88|error: within this context|
again\shape.h|32|error: 'int RectShape::height' is protected|
again\Entity.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 可以访问实体的受保护变量。 我需要在 Wall 中与 Entity 成为朋友!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.