命令push_back导致退出代码-103741819(0xC0000005)c++



我正试图用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, Team::POWERLIFTERS, 10, 2, 4, 5));
g1.addCharacter(GridPoint(6,1), Game::makeCharacter(CharacterType::SOLDIER, Team::CROSSFITTERS, 10, 2, 4, 5));
g1.addCharacter(GridPoint(6,4), Game::makeCharacter(CharacterType::MEDIC, Team::CROSSFITTERS, 10, 2, 4, 5));
std::cout << g1 << std::endl;
g1.move(GridPoint(1,1), GridPoint(1,2));
std::cout << g1 << std::endl;
std::cout << "Nice!" << std::endl;
}
int main() {
example1();
return 0;
}

它打印:

C:UsersUserCLionProjectsgameMakerCurr.1cmake-build-debugex0.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之后,向量将数据移动到内存中的一个新位置。之后,我们尝试使用it_src进行擦除,它仍然指向push_back之前的位置
因此,在执行此操作之前,以及在计算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。

最新更新