我有兩個類:拷貝構造函數不能識別繼承成員
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;
}
};
當我嘗試編譯包括該代碼,我得到一個錯誤消息說class multiEntity does not have any field named 'pos'
。除了entType
之外,這將發生在複製構造函數中的所有變量。 我試圖做的是有一個向量entity
s使用相同的SDL_Surface。因此,我覺得我應該創建一個單獨的類,其中具有相同entType
的每個對象具有相同的值spriteHandle
。這應該指向相同的圖像,當我有75個圖像試圖在屏幕上閃爍時,這是最有用的。我想使用vector的構造函數佈局,以便信息被複制,而不是每次都創建一個新的指針。
的可能的複製[初始化父母的保護成員初始化列表(C++)(https://stackoverflow.com/questions/2290733/initialize-parents-protected-members-with-initialization-list-c) –
你不需要發佈所有這些代碼來指出你遇到的問題。 [舉例](http://ideone.com/XAu2el) – PaulMcKenzie
謝謝@PaulMcKenzie。我會盡力記住這一點,並在將來更具描述性。 –