2017-02-27 11 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クラスとそれが機能は次のようになります: クラスアニメーション、そして公共その後、コンストラクタ:

#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; 
} 

// 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

は、コードの以下の最小限の、完全な例を考えてみましょう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.

我々は"mcve"呼ばanimListにキーを追加していないので、エントリが存在し、したがって、std::mapないものを挿入しようとすると、そこに問題があります:

あなたAnimationクラスのコンストラクタを宣言しているので、compiler will not automatically generate a default constructor。そのため、プログラムにはAnimationのデフォルトのコンストラクタが含まれていないため、コンパイルされません。

デフォルトのコンストラクタを追加し、または(要素への参照を取得するための要素、およびstd::map::findを追加するためにstd::map::insertを使用)std::map::operator[]への呼び出しを削除することによって、あなたの問題を解決することができます

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; 
} 
関連する問題