2016-02-14 120 views
0

我最近開始學習C++,並且我一直在這個錯誤上停留了一段時間。尋找類似的問題並沒有給我一個答案。 main.cpp中的沒有匹配函數調用'spriteSheet :: spriteSheet()'

部分:

#include <SDL.h> 
#include <SDL_image.h> 
#include <iostream> 
#include <string> 
#include "spritesheet.h" 
using namespace std; 

spriteSheet charimage("../img/char.png"); 
spriteSheet bgimage("../img/bg.png"); 

class playerSprite 
{ 
    public: 
     playerSprite(int xpos, int ypos, spriteSheet sheet); 

     int pos[2]; // player postition 
     spriteSheet image; 
}; 

playerSprite::playerSprite(int xpos, int ypos, spriteSheet sheet) 
{ 
    pos[0] = xpos; 
    pos[1] = ypos; 
    image = sheet; 
} 

playerSprite player(0, 0, charimage); 

spritesheet.h:

#include <SDL.h> 
#include <SDL_image.h> 
#include <iostream> 
#include <string> 
#include "spritesheet.h" 
using namespace std; 

spriteSheet::spriteSheet(string path) 
{ 
    SDL_Texture* newTexture = NULL; 
    SDL_Surface* loadedSurface = IMG_Load(path.c_str()); 

    if(loadedSurface == NULL) 
    { 
     cout << IMG_GetError() << endl; 
    } 
    else 
    { 
     newTexture = SDL_CreateTextureFromSurface(screen, loadedSurface); 
     if(newTexture == NULL) 
     { 
      cout << SDL_GetError() << endl; 
     } 
     else 
     { 
      ssize.w, sclip.w = loadedSurface->w; 
      ssize.h, sclip.h = loadedSurface->h; 
     } 
     SDL_FreeSurface(loadedSurface); 
    } 
    sheet = newTexture; 
} 

當我建我的playerSprite對象,我想:spritesheet.cpp的

#ifndef LTEXTURE_H 
#define LTEXTURE_H 
using namespace std; 

extern SDL_Renderer* screen; 

class spriteSheet //Texture wrapper for all game objects 
{ 
    SDL_Texture* sheet; 
    SDL_Rect ssize; 

    public: 
     spriteSheet(string path); 
     ~spriteSheet(); 
     void setClip(int x = 0, int y = 0, int w = 0, int h = 0); 
     void render(int x = 0, int y = 0); 
     void free(); 

     SDL_Rect sclip; 

}; 

#endif // LTEXTURE_H 

部分爲它傳入一個spriteSheet對象。我已經得到這個代碼來編譯通過像playerSprite :: setSheet()和spriteSheet :: setImage()函數,但我不能讓它與構造函數一起工作。每次我嘗試編譯我得到這個錯誤:

C:\Users\Platino\Documents\C++\Dragorogue\main.cpp In constructor 'playerSprite::playerSprite(int, int, spriteSheet)': 
20 65 C:\Users\Platino\Documents\C++\Dragorogue\main.cpp [Error] no matching function for call to 'spriteSheet::spriteSheet()' 
20 65 C:\Users\Platino\Documents\C++\Dragorogue\main.cpp [Note] candidates are: 
5 0 C:\Users\Platino\Documents\C++\Dragorogue\main.cpp In file included from main.cpp 
13 3 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] spriteSheet::spriteSheet(std::string) 
13 3 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] candidate expects 1 argument, 0 provided 
7 7 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] spriteSheet::spriteSheet(const spriteSheet&) 
7 7 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] candidate expects 1 argument, 0 provided 

編輯:

通過LogicStuff建議的方法會強迫我來初始化一個新的對象SpriteSheet每次我建立一個新的playerSprite對象的時間。我想創建一個SpriteSheet對象並讓所有playerSprite對象都使用它。

+0

可能[爲什麼我更喜歡使用成員初始化列表?](http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list) – LogicStuff

+0

首先,你呢?知道你爲什麼得到這個錯誤?或者你知道爲什麼,但問如何解決它? – PaulMcKenzie

+0

我不知道爲什麼我得到這個錯誤。有類似問題的人在構造它們的對象時已經搞亂了#include聲明或者傳遞了錯誤的參數,但是我不認爲我正在做這兩者中的任何一個。如果我刪除playerSprite類和播放器對象,我不會收到任何錯誤。 – DragonDePlatino

回答

0

由於您的spriteSheet對象沒有默認構造函數,因此問題在於playerSprite類中的spriteSheet成員需要使用一個參數構造。該成員是image

class spriteSheet 
{ 
     spriteSheet(string path); // <-- This is your constructor 
}; 

class playerSprite 
{ 
    public: 
     playerSprite(int xpos, int ypos, spriteSheet sheet); 
     spriteSheet image; // <-- This member has to be initialized on construction. 
}; 

在構造一個playerSprite的,所述spriteSheet構件必須被初始化。初始化image成員的唯一方法是與成員初始化列表中的一個參數來構建它:

playerSprite::playerSprite(int xpos, int ypos, spriteSheet sheet) : 
          image("somestring") 
{ 
    //... 
} 

注 - 構造函數(在{ }之間)的體內代碼初始化。這是任務 - 一個很大的區別。要初始化,您必須使用成員初始化列表。

此外,你應該通過const引用傳遞spriteSheet,不是值:一旦

playerSprite::playerSprite(int xpos, int ypos, const spriteSheet& sheet) : 
          image("somestring") 
{ 
    image = sheet; // now we can assign. 
} 

在構造函數中,那麼你可以做一個分配給成員:

Here is a small sample

+0

在您的建議中,我嘗試了成員初始化列表並更改了一小時的函數參數。既沒有工作,所以我回到成員函數而不是構造函數。感謝您的幫助,但讓這些構造函數工作遠遠超過它的價值。 – DragonDePlatino