如何解决命令push_back导致退出代码-10737418190xC0000005c ++
我正在尝试使用 C++ 制作一个小游戏,我必须将角色从地图上的一个点移动到另一个点。当我尝试通过 push_back 执行此操作然后从源点擦除时,我得到了此退出代码。我究竟做错了什么? 我的移动代码是:
void Game::move(const GridPoint & src_coordinates,const GridPoint & dst_coordinates) {
if(checkIfLegalCell(this,src_coordinates) == false ||
checkIfLegalCell(this,dst_coordinates) == false) {
throw IllegalCell();
}
if(searchInGrid(this->grid_characters,src_coordinates) == false){
throw CellEmpty();
}
std::vector<Pair>::iterator it_src=this->grid_characters.begin();
for(;it_src != this->grid_characters.end() ; ++it_src){
if((*it_src).grid_point == src_coordinates){
break;
}
}
if( ((*it_src).character)->checkIfCanMove(src_coordinates,dst_coordinates) == false) {
throw MovetooFar();
}
if(searchInGrid(this->grid_characters,dst_coordinates) == true){
throw CellOccupied();
}
this->grid_characters.push_back(Pair(dst_coordinates,(*it_src).character));
this->grid_characters.erase(it_src);
}
我的主要内容是这样的:
#include <iostream>
#include <cassert>
#include "Exceptions.h"
#include "Game.h"
using namespace mtm;
void example1() {
std::cout << "------example 1------" << std::endl;
Game g1(8,8);
g1.addCharacter(GridPoint(1,1),Game::makeCharacter(CharacterType::MEDIC,Team::POWERLIFTERS,10,2,4,5));
g1.addCharacter(GridPoint(1,4),Game::makeCharacter(CharacterType::SNIPER,5));
g1.addCharacter(GridPoint(6,Game::makeCharacter(CharacterType::SOLDIER,Team::CROSSFITTERS,5));
std::cout << g1 << std::endl;
g1.move(GridPoint(1,GridPoint(1,2));
std::cout << g1 << std::endl;
std::cout << "Nice!" << std::endl;
}
int main() {
example1();
return 0;
}
它打印:
C:\Users\User\CLionProjects\gameMakerCurr.1\cmake-build-debug\ex0.exe
------example 1------
*****************
| | | | | | | | |
| |M| | |N| | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| |s| | |m| | | |
| | | | | | | | |
*****************
Process finished with exit code -1073741819 (0xC0000005)
这是我用来创建-(坐标,字符)对的“游戏”的一部分
struct Pair {
GridPoint grid_point;
std::shared_ptr<Character> character;
Pair(GridPoint grid_point,std::shared_ptr<Character> character) :
grid_point(grid_point),character(character) {}
};
class Game {
std::vector<Pair> grid_characters; // character by grid point ; key = grid_point ; value = character.
int height;
int width;
解决方法
如果你仔细看,在程序中你试图将角色推到最后:
this->grid_characters.push_back(Pair(dst_coordinates,(*it_src).character));
this->grid_characters.erase(it_src);
首先,在向量中推送一个新元素后,它可能会重新分配数据以具有足够的容量来包含更多元素。但是什么时候 push_back 向量将数据移动到内存中的新位置。之后尝试使用指向 push_back 之前位置的 it_src,以将其擦除。
因此,在执行此操作之前,以及在计算 it_src 之前,您必须确保 vector 具有足够的容量并且不会重新定位您的数据
if (this->grid_characters.capacity() <= this->grid_characters.size())
this->grid_characters.reserve(this->grid_characters.size() + 10);
//other variants
//this->grid_characters.reserve(this->grid_characters.size() + 1);
//this->grid_characters.reserve(this->grid_characters.size() * 2);
那应该可以修复失败。
但从逻辑上讲,您为什么要尝试将对象移动到最后?您将坐标从 1,1 更改为 1,2。在向量中,它将位于坐标为 6,4 的对象之后。看起来向量中的顺序无关紧要。如果对象的顺序没有多大意义,那么只改变坐标是有意义的,而不是在向量内移动它
it_src->grid_point = dst_coordinates;
如果顺序很重要,请使用 set。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。