-3

So I've been following the video tutorial for developing a game in C++ using SFML and run into an error. I've described the problem in this site: http://en.sfml-dev.org/forums/index.php?topic=21589.0 (I know I know it's not good to share links but well I don't plan to remove the thing in near future.) For the highlight the error is: C:/Program Files/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/bits/stl_map.h:504:59: error: no matching function for call to 'Animation::Animation()'

The line that's making the conflict I think is: std::map animList;

My Animation class and it's function looks like this: Class Animation, then public and then the constructor:

// 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;}
};
  • _The line that's making the conflict I think is: std::map animList;_ that line literally doesn't appear in your code – Tas Feb 27 '17 at 03:52
  • Did you check the link, there's more of the code. –  Feb 27 '17 at 03:59
  • No. Did you check [ask]? Namely, creating a [mcve] – Tas Feb 27 '17 at 04:01
  • Sorry, that looks snarky, but it's not meant to be -_- just providing relevant links. The relevant code should always appear within your question, not on third-party sites that may go down etc – Tas Feb 27 '17 at 04:02
  • It would expand into a huge block of code, but I guess it's fine then. –  Feb 27 '17 at 04:40
  • Did you try reading the error message? You don't have a default constructor for `Animation`, yet you expect to use it in with `std::map::operator[]`? You'll need to change one or the other. – Toby Speight Feb 27 '17 at 13:09

1 Answers1

2

Consider the following minimal, complete example of your 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;
}

When we call animList["mcve"].frames, we are calling std::map::operator[], which has this to say (emphasis mine):

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.

Since we haven't added a key to animList called "mcve", the entry does not exist and therefore std::map will attempt to insert one, and therein lies the problem:

Because you have declared a constructor for your Animation class, the compiler will not automatically generate a default constructor. Therefore your program does not contain a default constructor for Animation, and thus won't compile.

You can fix your problem by adding a default constructor, or removing calls to std::map::operator[] (use std::map::insert to add elements, and std::map::find to retrieve a reference to an element):

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;
}
Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51