C++ 向量加减抛出"expression: vector subscript out of range."错误

C++ Vectors adding and subtracting throwing out "expression: vector subscript out of range." error

本文关键字:subscript vector out of 错误 range expression 向量 C++      更新时间:2023-10-16

我的代码有点麻烦。我制作了一个 10x10 的网格,玩家可以在其中四处移动。在这个网格中是敌人和某些事件。我使用了一个向量来存储这些事件。例如,如果玩家点击 10x10 网格数组中的图块"e",则玩家会遇到敌人,并且矢量中的"e"将被还原,这样就不会在该图块上再次发生,而是显示"走廊"。这适用于"e"磁贴(第 147 行(,但是当我尝试复制"i"(第 133 行(磁贴的代码时,它会中断并出现错误"表达式:矢量下标超出范围"。我不知道它为什么要这样做,我对编码很陌生。如果您启动游戏并移动到 X:10 Y4,您将看到错误。我非常感谢任何反馈或建议。抱歉,如果代码也很长,我试图尽可能压缩它。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
int nextX;
int nextY; 
int playerX = 1;
int playerY = 1;
std::string name;
std::string move;
bool win = false;
std::vector<std::string> playerPrivInv;
void movePlayer(int prevX, int prevY);
void location(int X, int Y);
void game();

char dungeon[12][12] = {
{ 'w','w','w','w','w','w','w','w','w','w','w','w'},
{ 'w','s','c','c','c','c','w','c','c','c','r','w'},
{ 'w','c','w','w','w','c','w','c','w','w','w','w'},
{ 'w','D','w','c','w','c','w','c','w','w','w','w'},
{ 'w','R','w','c','w','c','d','e','c','c','i','w'},
{ 'w','w','w','c','w','c','w','w','w','w','w','w'},
{ 'w','w','c','c','c','c','c','c','c','w','r','w'},
{ 'w','w','c','w','w','w','w','c','w','c','c','w'},
{ 'w','w','c','w','i','c','c','c','c','c','b','w'},
{ 'w','w','w','w','w','w','w','c','w','w','w','w'},
{ 'w','f','c','c','c','c','G','c','w','w','w','w'},
{ 'w','w','w','w','w','w','w','w','w','w','w','w'}
};
int main()
{
playerPrivInv.push_back("e"); // 0
playerPrivInv.push_back("x"); // 1
playerPrivInv.push_back("k"); // 2
playerPrivInv.push_back("i"); // 3
game();
}
void game() {
do {
location(playerX, playerY);
movePlayer(playerX, playerY);
} while (win == false);
}
void movePlayer(int prevX, int prevY) {
std::cout << "You're coordinates are X: " << prevX << " Y: " << prevY << std::endl;

std::cin >> move;
if (move == "n" || move == "north") {
nextY = playerY - 1;
if ((nextY >= 0) && (nextY < 12)) {
playerY = nextY;
}
else {
std::cout << "Can't move there" << std::endl;
}
}
else if (move == "e") {
nextX = playerX + 1;
if ((nextX >= 0) && (nextX < 12)) {
playerX = nextX;
}
else {
std::cout << "Can't move there" << std::endl;
}
}
else if (move == "w") {
nextX = playerX - 1;
if ((nextX >= 0) && (nextX < 12)) {
playerX = nextX;
}
else {
std::cout << "Can't move there" << std::endl;
}
}
else if (move == "s") {
nextY = playerY + 1;
if ((nextY >= 0) && (nextY < 12)) {
playerY = nextY;
}
else {
std::cout << "Can't move there" << std::endl;
}
}
system("CLS");
}
void location(int X, int Y) {
char local = dungeon[Y][X];
// S: Starter Room
// F: Final Room
// D: Door
// C: Corridor
// R: Room
// W: Wall
// E: Enemy
// X: Enemy
// K: Enemy
switch (local) {
case 'i':
if (playerPrivInv[3] == "i") {
playerPrivInv.erase(playerPrivInv.begin() + 3);
std::cout << "Interogated!" << std::endl;
}
else {
std::cout << "Corridor" << std::endl;
}
break;
case 'r':
std::cout << "Room" << std::endl;
break;
case 'e':
if (playerPrivInv[0] == "e") {
playerPrivInv.erase(playerPrivInv.begin() + 0);
std::cout << "Enemy" << std::endl;
}
else {
std::cout << "Corridor" << std::endl;
}
break;
case 's':
std::cout << "Starter Room" << std::endl;
break;
case 'f':
std::cout << "Godrik's Finale" << std::endl;
break;
case 'd':
std::cout << "There's a door in the way, its unlocked..." << std::endl;
break;
case 'D':
if (move == "s" || move == "south") {
nextY = playerY - 1;
if ((nextY >= 0) && (nextY < 12)) {
playerY = nextY;
}
}
std::cout << "There's a door in the way, its locked..." << std::endl;
std::cout << "You need to find a key..." << std::endl;
std::cout << std::endl;
break;
case 'c':
std::cout << "Corridor" << std::endl;
break;
case 'R':
std::cout << "You enter a dark office..." << std::endl;
std::cout << "Room" << std::endl;
break;
case 'w':
if (move == "e" || move == "east") {
std::cout << "That's a Wall" << std::endl;
nextX = playerX - 1;
if ((nextX >= 0) && (nextX < 12)) {
playerX = nextX;
}
}
else if (move == "w" || move == "west") {
std::cout << "That's a Wall" << std::endl;
nextX = playerX + 1;
if ((nextX >= 0) && (nextX < 12)) {
playerX = nextX;
}
}
else if (move == "s" || move == "south") {
std::cout << "That's a Wall" << std::endl;
nextY = playerY - 1;
if ((nextY >= 0) && (nextY < 12)) {
playerY = nextY;
}
}
else if (move == "n" || move == "north") {
std::cout << "That's a Wall" << std::endl;
nextY = playerY + 1;
if ((nextY >= 0) && (nextY < 12)) {
playerY = nextY;
}
}
}
}

当你处理'e'的情况时,你从playerPrivInv中删除其中一个元素,留下3个元素。稍后,当您尝试处理'i'的情况时,您尝试访问playerPrivInv[3]会导致越界运行时错误,因为该向量中只有 3 个元素,而不是 4 个。

可能的解决方案包括将playerPrivInv中的元素更改为其他内容(如空字符串(,而不是将其从向量中删除,或者在向量中搜索匹配的字符串,而不是假设它位于特定位置。