简体   繁体   中英

Protected variables in C++

So, working with some classes and inheritance, I created an Entity class which inherits from an ImageShape class which inherits from an abstract Shape class. I also have a Wall class inheriting from a RectShape class, which inherits from the same abstact Shape class.

I need to work around collisions, so I called Wall to be Entity's friend class and went on to create a function using Entity's protected x, y, newx, newy fields and Wall's protected x, y, width, height fields.

when compiling, the compiler tells me Entity's variables are protected, but from my understanding I should be able to use them if Wall is friend of Entity;

Here are my classes: 1. Entity class

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.Wall class

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.This is the collision function

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

also, here are part of the errors I get:

||=== 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)) ===|

Argh, I got it. Friending Wall meant that Wall could access Entity's protected variables. I needed to friend Entity in Wall instead!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM