1

I have two classes:

class entity {
public:
  SDL_Rect pos;
  float x;
  float y;

  std::string spriteFile;
  SDL_Surface * spriteHandle;
  int rePos (int newX, int newY);
  int move (float deltaX, float deltaY, bool check); // Move x & y the specified amounts, also moving pos.x and pos.y for every integer increase of x or y
  int display (SDL_Surface * screenSurface); //Use SDL_BlipSurface to blip the image to the screen
  int loadImage (SDL_PixelFormat * format); //Load the image using the spriteFile string and optimize it using the SDL_PixelFormat of the Screen

  entity (std::string file, int w, int h);
  entity () {};
  ~entity () { SDL_FreeSurface(spriteHandle);}

  entity (const entity &old) : pos(old.pos), x(old.x), y(old.y), spriteFile(old.spriteFile) {
  spriteHandle = new SDL_Surface(*spriteHandle);
  }

};

class multiEntity: public entity {
  /*
    Use multiEntity rather than entity when multiple images need to be blipped to different
    positions.
   */
private:
  static std::vector<stringBoolPair> deconstructed;
public:
  std::string entType;
  multiEntity (std::string file, int w, int h, std::string enttype) {
    entity(file, w, h);
    entType = enttype;
    bool found = false;
    for (int i = 0; i < deconstructed.size(); i++) {
      found = (enttype == deconstructed[i].str);
    }
    if (found) deconstructed.emplace_back(enttype, false);
  }

  multiEntity (const multiEntity& old) :
    spriteFile(old.spriteFile),
    x(old.x),
    y(old.y),
    spriteHandle(old.spriteHandle),
    pos(old.pos),
    entType(old.entType) {}
    ~multiEntity () {
    for (int i = 0; i < deconstructed.size(); i++) {
      if (deconstructed[i].str == entType) {
    SDL_FreeSurface(spriteHandle);
    deconstructed[i].Bool = true;
      }
    }
  }

  multiEntity& operator= (const multiEntity& old) {
    spriteFile = old.spriteFile;
    pos = old.pos;
    x = old.x;
    y = old.y;
    entType = old.entType;
    spriteHandle = old.spriteHandle;
    return *this;
  }
};

When I attempt to compile the code that includes this, I get an error messages saying that class multiEntity does not have any field named 'pos'. This happens for all of the variables in the copy constructor except for entType. What I'm attempting to do is have a vector of entitys using the same SDL_Surface. Therefore, I felt that I should create a separate class where every object with the same entType has the same value for spriteHandle. This is supposed to point to the same image, which is most useful when I have 75 instances of an image that I'm trying to blip to the screen. I want to use vector's constructor layout so that the information is copied over rather than creating a new pointer each time.

  • 1
    Possible duplicate of [Initialize parent's protected members with initialization list (C++)](https://stackoverflow.com/questions/2290733/initialize-parents-protected-members-with-initialization-list-c) – Weak to Enuma Elish Jun 03 '17 at 02:13
  • You didn't need to post all of this code to point out the issue you're having. [An example](http://ideone.com/XAu2el) – PaulMcKenzie Jun 03 '17 at 02:16
  • Thanks @PaulMcKenzie. I'll try to remember that and be more descriptive in the future. –  Jun 03 '17 at 02:21

1 Answers1

2

You can't initialize the base class's members in derived class's copy constructor; they should be initialized via base class's copy constructor. You should just invoke it in member initalizer list:

multiEntity (const multiEntity& old) :
    entity(old),         // initialize entity's members via entity::entity(const entity&)
    entType(old.entType) // initialize multiEntity's member entType 
{}

If the behavior of copy constructor of the base class is not what you want, you can do some assignment in the constructor body of the derived class, e.g.

multiEntity (const multiEntity& old) :
                         // nothing specified, same as write entity() here
                         // entity's members will be initialized via entity::entity()
    entType(old.entType) // initialize multiEntity's member entType 
{
    // perform assignment here
    spriteFile = old.spriteFile;
    x = old.x;
    y = old.y;
    spriteHandle = old.spriteHandle;
    pos = old.pos;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • How then would I insure that the SDL_Surface is a shallow copy? I want it to copy the data in the parent class, but only copy the address in the child class. –  Jun 03 '17 at 02:25
  • @BobPaw Answer revised. – songyuanyao Jun 03 '17 at 02:32