命令 push_back 导致退出代码 -1073741819 (0xC0000005) c++
command push_back results an exit code -1073741819 (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:\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);
首先,在您将一个新元素压入 vector 后,它可能会重新分配数据以具有足够的容量来包含更多元素。所以在 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;
如果顺序很重要,请使用集合。
我正在尝试使用 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:\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);
首先,在您将一个新元素压入 vector 后,它可能会重新分配数据以具有足够的容量来包含更多元素。所以在 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;
如果顺序很重要,请使用集合。