C ++中的受保护变量

问题描述 投票:0回答:1

因此,通过使用某些类和继承,我创建了一个Entity类,该类从ImageShape类继承,该ImageShape类从一个抽象Shape类继承。我也有一个Wall类,它继承自RectShape类,而RectShape类继承自相同的abstact Shape类。

我需要解决冲突,因此我将Wall称为Entity的朋友类,然后继续使用Entity的受保护的x,y,newx,newy字段和Wall的受保护的x,y,width,height字段来创建函数。

[编译时,编译器告诉我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)) ===|
c++ class sdl-2
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.