2017-02-27 89 views
-3

所以我一直在關注使用SFML開發C++遊戲的視頻教程,並且遇到錯誤。我在這個網站上描述了這個問題: http://en.sfml-dev.org/forums/index.php?topic=21589.0 (我知道我知道分享鏈接並不好,但我不打算在不久的將來刪除它。) 對於突出顯示的錯誤是: C :/ Program Files/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/C++/bits/stl_map.h:504:59:錯誤:沒有匹配函數調用'Animation :: Animation()'沒有匹配的函數調用'Animation :: Animation()

我認爲是衝突的線是: std :: map animList;

我的動畫類,它的功能是這樣的: 類動畫,然後公衆然後構造:

// Animation class 
class Animation 
{ 
public: 
    std::vector<IntRect> Frames, Frames_flip; 
    float CurrentFrame, Speed; 
    bool Flip, IsPlaying; 
    Sprite sprite; 

Animation(Texture &t, int x, int y, int w, int h, int Count, float speed, int Step) 
{ 
    Speed = speed; 
    sprite.setTexture(t); 

    CurrentFrame = 0; 
    IsPlaying = true; 
    Flip = false; 


    for (int i = 0; i < Count; i++) 
    { 
     Frames.push_back(IntRect(x+i*Step,y,w,h)); 
     Frames_flip.push_back(IntRect(x+i*Step+w,y,-w,h)); 
    } 
} 


void Tick(float Time) 
{ 
    if (!IsPlaying) return; 

    CurrentFrame += Speed * Time; 

    if (CurrentFrame> Frames.size()) 
     CurrentFrame -= Frames.size(); 


    int i = CurrentFrame; 

    sprite.setTextureRect(Frames[i]); 
    if (Flip) sprite.setTextureRect(Frames_flip[i]); 

    } 

}; 

// Animation Manager Class 

class AnimationManager 
{ 
public: 
    String CurrentAnim; 

std::map<String, Animation> animList; 

AnimationManager() 
{ 

} 

void Create(String Name, Texture &t, int x, int y, int w, int h, int Count, float Speed, int Step) 
{ 
    animList[Name] = Animation(t,x,y,w,h,Count,Speed,Step); 
    CurrentAnim = Name; 
} 


void Draw(RenderWindow &window, int x = 0, int y = 0) 
{ 
    animList[CurrentAnim].sprite.setPosition(x,y); 
    window.draw(animList[CurrentAnim].sprite); 
} 

void Set(String name) { CurrentAnim = name; } 

void flip (bool b) { animList[CurrentAnim].Flip = b; } 

void tick(float Time) {animList[CurrentAnim].Tick(Time); } 

void pause() {animList[CurrentAnim].IsPlaying = false;} 

void play() {animList[CurrentAnim].IsPlaying = true;} 
}; 
+0

_The行多數民衆贊成使得衝突我認爲是:std :: map animList; _該行幾乎不會出現在您的代碼中 – Tas

+0

您是否檢查了鏈接,還有更多的代碼。 –

+0

不,你檢查[問]嗎?也就是說,創建一個[mcve] – Tas

回答

2

考慮你的代碼的下面最小的,完整的例子:

#include <iostream> 
#include <map> 
#include <string> 

struct Animation 
{ 
    Animation(size_t totalFrames) : frames(totalFrames) {} 
    size_t frames; 
}; 

int main() 
{ 
    std::map<std::string, Animation> animList; 
    std::cout << animList["mcve"].frames << std::endl; 
} 

當我們撥打animList["mcve"].frames時,我們打電話給std::map::operator[],有這個說法(重點是我的):

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

mapped_type must meet the requirements of CopyConstructible and DefaultConstructible.

因爲我們還沒有加入一鍵animList"mcve",該條目不存在,因此std::map將嘗試插入之一,就是問題所在:

因爲你已經聲明瞭一個構造函數爲您Animation類,compiler will not automatically generate a default constructor。因此,您的程序不包含Animation的默認構造函數,因此無法編譯。

您可以通過添加默認的構造函數,或刪除調用std::map::operator[]解決您的問題(使用std::map::insert添加元素,std::map::find檢索引用元素):

std::map<std::string, Animation> animList; 
animList.insert({"mcve", 10}); 
auto it = animList.find("mcve"); 
if (it != animList.end()) 
{ 
    std::cout << it->second.frames << std::endl; 
}