调用 'Animation::Animation() 时没有匹配函数
No matching function for call to 'Animation::Animation()
因此,我一直在关注视频教程,用于使用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;
}
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 调用 'Animation::Animation() 时没有匹配函数