2017-06-03 54 views
1

我有兩個類:拷貝構造函數不能識別繼承成員

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的構造函數佈局,以便信息被複制,而不是每次都創建一個新的指針。

+1

的可能的複製[初始化父母的保護成員初始化列表(C++)(https://stackoverflow.com/questions/2290733/initialize-parents-protected-members-with-initialization-list-c) –

+0

你不需要發佈所有這些代碼來指出你遇到的問題。 [舉例](http://ideone.com/XAu2el) – PaulMcKenzie

+0

謝謝@PaulMcKenzie。我會盡力記住這一點,並在將來更具描述性。 –

回答

2

您無法在派生類的拷貝構造函數中初始化基類的成員;它們應該通過基類的拷貝構造函數初始化。你應該只調用它的成員名單initalizer:

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

如果基類的拷貝構造函數的行爲是不是你想要的,你可以在派生類,如的構造函數體做一些分配

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

那我該如何確保SDL_Surface是淺拷貝?我希望它複製父類中的數據,但只複製子類中的地址。 –

+0

@BobPaw回答修改。 – songyuanyao