【问题标题】:No matching function for call to 'Animation::Animation()调用 'Animation::Animation() 没有匹配的函数
【发布时间】:2017-02-27 03:49:49
【问题描述】:

所以我一直在关注使用 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()'

我认为造成冲突的那一行是: std::map 动画列表;

我的 Animation 类及其功能如下所示: 类 Animation,然后是 public,然后是构造函数:

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

【问题讨论】:

  • 我认为造成冲突的那一行是:std::map animList; 那一行字面上并没有出现在你的代码中
  • 你检查链接了吗,还有更多的代码。
  • 没有。你检查How to Ask了吗?即,创建一个minimal reproducible example
  • 抱歉,这看起来很刻薄,但这并不意味着 -_- 只是提供相关链接。相关代码应始终出现在您的问题中,而不是出现在可能出现故障等的第三方网站上
  • 它会扩展成一个巨大的代码块,但我想这样就可以了。

标签: c++ codeblocks sfml


【解决方案1】:

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

#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[],这是要说的(强调我的):

返回对映射到与 key 等效的键的值的引用,如果该键不存在则执行插入

mapped_type 必须满足 CopyConstructible 和 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;
}

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多