C++ c+中的动画师+;使用SDL2

C++ c+中的动画师+;使用SDL2,c++,arrays,dictionary,sdl,sdl-2,C++,Arrays,Dictionary,Sdl,Sdl 2,我尝试创建一个贴图,其中关键点是一个枚举,值是一个SDL_Rect数组(包含)动画的所有坐标 std::map<Animation, SDL_Rect*> animations; Animation currAnim; 我猜您使用的数组超出了范围,因此存储在映射中的指针不再有效 由于您没有发布所有代码,建议您不要将指针作为数据存储在映射中。相反,请使用std::vector #include <map> #include <vector> typedef

我尝试创建一个贴图,其中关键点是一个枚举,值是一个SDL_Rect数组(包含)动画的所有坐标

std::map<Animation, SDL_Rect*> animations; 
Animation currAnim;

我猜您使用的数组超出了范围,因此存储在映射中的指针不再有效

由于您没有发布所有代码,建议您不要将指针作为数据存储在映射中。相反,请使用
std::vector

#include <map>
#include <vector>
typedef std::map<Animation, std::vector<SDL_Rect>> AnimationMap;
//...
AnimationMap animations;
//...
std::vector<SDL_Rect> right(3);
right[0].x = 264;
right[0].y = 0;
right[0].w = 36;
right[0].h = 64;
//...
animations.insert(std::make_pair(Animation::MoveRight, right));
//...
#包括
#包括
typedef std::map AnimationMap;
//...
动画地图动画;
//...
std::向量右(3);
右[0]。x=264;
右[0],y=0;
右[0],w=36;
右[0],h=64;
//...
insert(std::make_pair(Animation::MoveRight,right));
//...

如果向量
右侧
超出范围,则地图仍然正常,因为向量的副本放置在地图的数据部分

不工作意味着什么?编译错误(包括错误)、运行时错误(是的,包括错误)等等。当您只存储指向其中一个的指针时,为什么要说“SDL_Rect数组”?即便如此,为什么要使用指针
std::map
或者如果真正的数组
std::map
@LewisBolender
出现异常并且程序崩溃
那么,您使用的指针我看不到需要它们。光是这一点就可能是该计划出现问题的原因之一。可能那个数组超出了作用域,现在地图上出现了垃圾数据。如果是这种情况,请使用
std::vector
作为映射的键,而不要使用
SDL\Rect*
作为映射中的数据。通过使用
SDL\u Rect*
作为数据,您将该映射绑定到该数组。如果该数组超出范围,则映射具有无效值。此外,请发布更多代码并解释异常发生在哪一行(而不是说“它不工作”)。除非发布更多的代码,否则没有人能给你一个明确的答案。
void Player::Draw(SDL_Renderer * renderer, int frame)
{
    std::vector<SDL_Rect>::iterator it = std::find(animator->GetAnimations()[animator->GetCurrAnim()].begin(),
        animator->GetAnimations()[animator->GetCurrAnim()].end(), frame);

    SDL_RenderCopy(renderer, lTexture->GetTexture(), &*it, &rect);
}
'==' There is no operator found which accept type "SDL_Rect"
#include <map>
#include <vector>
typedef std::map<Animation, std::vector<SDL_Rect>> AnimationMap;
//...
AnimationMap animations;
//...
std::vector<SDL_Rect> right(3);
right[0].x = 264;
right[0].y = 0;
right[0].w = 36;
right[0].h = 64;
//...
animations.insert(std::make_pair(Animation::MoveRight, right));
//...