2017-02-27 2 views
-3

J'ai donc suivi le tutoriel vidéo pour développer un jeu en C++ en utilisant SFML et rencontrer une erreur. J'ai décrit le problème dans ce site: http://en.sfml-dev.org/forums/index.php?topic=21589.0 (Je sais que je sais que ce n'est pas bon de partager des liens mais bien je ne prévois pas d'enlever la chose dans un avenir proche.) Pour le point culminant l'erreur est: C :/Program Files/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/C++/bits/stl_map.h: 504: 59: erreur: aucune fonction correspondante pour l'appel à 'Animation :: Animation()'Aucune fonction correspondante pour l'appel de 'Animation :: Animation()

La ligne qui crée le conflit est: std :: map animList;

Ma classe d'animation et sa fonction ressemble à ceci: d'animation de classe, puis public, puis le constructeur:

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

_La ligne qui est fait du conflit Je pense est: std :: map animList; _ cette ligne n'apparaît pas littéralement dans votre code – Tas

+0

Avez-vous vérifié le lien, il y a plus de code. –

+0

Non. Avez-vous vérifié [demander]? À savoir, la création d'un [mcve] – Tas

Répondre

2

Prenons l'exemple minimal, complet suivant de votre code:

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

Lorsque nous appelons animList["mcve"].frames, nous appelons std::map::operator[], qui a ceci à dire (emphase mienne):

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.

Puisque nous n'avons pas ajouté une clé pour animList appelé "mcve", l'entrée n'existe pas et donc std::map va tenter d'insérer un, et le problème est là:

Parce que vous avez déclaré un constructeur pour votre Animation classe , le compiler will not automatically generate a default constructor. Par conséquent votre programme ne contient pas un constructeur par défaut pour Animation, et ne compilera donc pas.

Vous pouvez corriger votre problème en ajoutant un constructeur par défaut, ou la suppression des appels à std::map::operator[] (utiliser std::map::insert pour ajouter des éléments et std::map::find pour récupérer une référence à un élément):

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