矢量范围问题
Vector range issues
所以我试图创建一种保存/跳跃系统,玩家可以在其中将世界的某些方面恢复到前一点。由于某些原因,以下代码会产生矢量错误。("矢量下标超出范围")
(entityList是ptr,recordedEntityList不是)
void Map::record()
{
for(unsigned int x = 0; x < entityList.size(); x++)
{
if(entityList[x]->getRewind() == true)
{
recordedEntityList.push_back(*entityList[x]);
printf("%f, %fn", entityList[x]->getSprite().getPosition().x, entityList[x]->getSprite().getPosition().y);
}
}
}
void Map::rewind()
{
for(unsigned int x = 0; x < entityList.size(); x++)
{
if(entityList[x]->getRewind() == true)
{
entityList.erase(entityList.begin() + x);
}
}
for(unsigned int y = 0; y < recordedEntityList.size(); y++)
{
entityList.push_back(&recordedEntityList[y]);
}
recordedEntityList.clear();
}
在rewind
中,将指向recordedEntityList
元素的指针推入entityList
,然后清除recordedEntityList
。这将导致entityList
包含无效的指针,并在访问指针时调用未定义的行为。
此外,与此错误无关,在rewind
的第一个循环中迭代向量时修改向量的方式可能会导致跳过条目:如果两个连续条目的getRewind()
返回true
,则第二个条目不会被删除。
相关文章:
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 使用基于范围的循环迭代矢量时出现问题
- 示例代码中使用分隔符将 std::string 拆分为 std::vector 的范围问题
- C++ 字符串订阅超出范围的问题
- 命名空间范围问题
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 断言失败错误,C++中的矢量下标超出范围问题
- 给定数组范围的选择排序问题
- 指针范围问题和返回类中封装的指针向量内的指针引用
- C++范围?将数组传递给函数时出现问题
- C++奇怪的问题.数据成员超出范围
- PlatformIO 的范围问题(?)
- C++. 面试. 关于返回 int* 值问题的范围
- 编程:原理与实践 使用C++ 第 4 章演练步骤 6:关于数字范围的一般问题
- 为什么BKDFHash不关心超出范围的问题?
- 访问函数中的使用声明:与范围相关的问题
- 函数返回本地变量,尽管变量不超出范围,没有编译器问题,并且代码执行
- 无法编译 - 范围问题