我从下面的代码得到一个程序崩溃:C++图矢量参考误差
// declaration in header:
std::vector<Animation> mAnimations; // animation objects
std::map<std::string, Animation*> mBoneAnimations; // map strings to animation objects from "mAnimations"
// source code in source file:
this->mAnimations.push_back(Animation());
this->mBoneAnimations[bone_name] = &this->mAnimations.back();
// ...
// at different points, we modify the LAST element in animation
this->mAnimations.back().addTranslation(Vector3(...));
// this is where the problem occurs later on in the program:
Animation test = *(this->mBoneAnimations.at("Body")); // CRASH
在最后一行此代码崩溃。我80%确定这个问题是由我设置这个数据结构的方式造成的。我的猜测是,也许使用“back()”引用矢量中的最后一个对象可能会导致这种情况。我试图改变它:
this->mBoneAnimations[bone_name] = &this->mAnimations[this->mAnimations.size() - 1];
但这也没有帮助。问题是导致这次事故的原因,更具体地说,我该如何解决这个问题?
编辑:我确定那里有一个“Body”对象。这次崩溃是一个内存错误。
您确定地图中存在“正文”键吗?并且是动画可复制的?顺便说一句,“身体”是不一样的“身体”(它是区分大小写) –
@HumamHelfawi非常感谢你的提示,但不幸的是我确信。 – Jas