调用 'Animation::Animation() 时没有匹配函数

No matching function for call to 'Animation::Animation()

本文关键字:Animation 函数 调用      更新时间:2023-10-16

因此,我一直在关注视频教程,用于使用SFML在C 中开发游戏并遇到错误。我已经在此网站中描述了问题:http://en.sfml-dev.org/forums/index.php?topic=21589.0(我知道我知道分享链接不好,但是我不打算在不久的将来删除这件事。)对于突出显示,错误是:c:/program文件/codeBlocks/mingw/lib/gcc/mingw32/4.9.2/includ/c /stl_map.h:504:59:59:59:59:错误:无匹配的匹配函数'animation :: Animation :: Animation()'andimation()'

我认为是冲突的线路是: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;}
};

考虑以下最小,完整的代码示例:

#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[],这说明了这一点(强调我的):

返回对映射到相当于键的键的值的引用,如果尚不存在此类密钥,则执行插入

mapped_type必须满足CopyConstructible和DefaultConstructibled的要求。

由于我们没有在animList中添加一个称为"mcve"的键,因此该条目不存在,因此std::map将尝试插入一个,因此存在问题:

由于您已声明了Animation类的构造函数,因此编译器不会自动生成默认的构造函数。因此,您的程序不包含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;
}