2012-03-18 53 views
1

我有一個名爲SpriteCollection的類,我用SDL加載圖像。這個班有一個屬性叫:數組分配

SDL_Surface* sprites[]; 

我認爲這是正確的(雖然我不確定)。在同一個類我有一個方法:

void addNewSprite(SDL_Surface* sprite){ 

    this->sprites[n_sprites+1] = new SDL_Surface; 
    this->sprites[n_sprites+1] = IMG_Load("spritepath.jpg"); 
    this->n_sprites++; 
} 

,另一個檢索SDL_Surface在屏幕上繪製:

SDL_Surface getSprite(int sprite_index){ 

    return this->sprites[sprite_index]; 
} 

並以我使用的屏幕上繪製:

Draw(x_position, y_position, this->sprite->getSprite[0], screen); 

我正在加載圖片;一切正常,但IDE返回關於指針和SDL_SurfaceSDL_Surface *之間轉換的錯誤。

我在做什麼錯?

編輯:錯誤消息:

E:\cGame.cpp|71|error: cannot convert `SDL_Surface' to `SDL_Surface*' for argument `1' to `int SDL_UpperBlit(SDL_Surface*, SDL_Rect*, SDL_Surface*, SDL_Rect*)'| 
+0

請發佈您正在收到的* exact *錯誤消息。 – 2012-03-18 15:58:34

回答

5

在你getSprite功能,具有SDL_Surface返回類型,你試圖返回SDL_Surface* 。也許你的意思是:

SDL_Surface* getSprite(int sprite_index){ 
    return this->sprites[sprite_index]; 
} 

此外,這些線是非常可疑:

this->sprites[n_sprites+1] = new SDL_Surface; 
this->sprites[n_sprites+1] = IMG_Load("spritepath.jpg"); 

首先你動態分配一個新的SDL_Surface和指針存儲到它。然後,通過將IMG_Load調用的結果分配給它,您將擺脫該指針。現在你永遠無法完成表面,因爲你失去了指向它的指針。您可以考慮封裝您的精靈並使用RAII成語來處理SDL_Surface的分配。

最重要的是,您最好使用std::vector而不是陣列。

3
SDL_Surface getSprite(int sprite_index) 

應該是:

SDL_Surface* getSprite(int sprite_index)