C++矢量下标超出范围
C++ Vector subscript out of range
据我所知,这个错误通常发生在迭代器越界时,但我在空向量上遇到了这个问题。
这是保存矢量的Player类:
Player.h:
#pragma once
#include "Item.h"
#include "Room.h"
#include <vector>
#include <memory>
#include <string>
using namespace std;
class Player
{
private:
int health;
string name;
shared_ptr<Room> currentRoom;
vector<shared_ptr<Item> > inventory;
public:
Player(void);
Player(string);
~Player(void);
void changeHealth(int);
void setRoom(shared_ptr<Room>);
shared_ptr<Room> getRoom();
unsigned int getInventorySize();
};
Player.cpp:
#include "Player.h"
#include <iostream>
#include <memory>
#include <string>
Player::Player(void)
{
health = 20;
}
Player::Player(string newName)
{
name = newName;
}
Player::~Player(void)
{
}
void Player::changeHealth(int amount){
health += amount;
}
void Player::setRoom(shared_ptr<Room> newRoom){
currentRoom = newRoom;
}
shared_ptr<Room> Player::getRoom(){
return currentRoom;
}
unsigned int Player::getInventorySize(){
return inventory.size();
}
我评论掉了Player中影响库存的所有其他方法。总的来说,我制作了一个播放器对象,但我不做任何库存操作,所以它应该是完全空的,getInventorySize应该返回0,对吧?但当我这样做时:
cout<<player->getInventorySize();
程序因矢量下标超出范围错误而崩溃。发生了什么事
完整主干:
#include "World.h"
#include "Player.h"
#include "Room.h"
#include "Item.h"
#include "Option.h"
#include "RoomOption.h"
#include <iostream>
#include <memory>
#include <string>
#include <stdlib.h>
typedef shared_ptr<Option> optionPtr;
typedef shared_ptr<Item> itemPtr;
typedef shared_ptr<Room> roomPtr;
const int healthPackID =0;
World world;
void createWorld(void);
void main(){
createWorld();
shared_ptr<Player> player = world.getPlayer();
int selection = 0, inventoryOption = 0, exitOption = 0;
do{
inventoryOption = player->getRoom()->getNumOptions() + 1;
exitOption = inventoryOption + 1;
cout<<player->getRoom()->getDescription()<<endl;
player->getRoom()->printOptions();
cout<< inventoryOption <<". View Inventory"<<endl;
cout<< exitOption <<". Quit game"<<endl<<endl;
cin>>selection;
string optionType = typeid(*(player->getRoom()->getOption(selection- 1).get())).name();
if(selection == inventoryOption){
cout<<player->getInventorySize();
}
else if( optionType.compare("class RoomOption") == 0){
player->setRoom(player->getRoom()->getOption(selection- 1)->getRoom());
cout<< "RoomOption!"<<endl;
}
else{
cout<< "Not RoomOption =("<<endl;
}
system("CLS");
}while(selection != exitOption);
}
void createWorld(){
shared_ptr<Player> player(new Player("Ted"));
world.setPlayer(player);
roomPtr outside(new Room(0, "You're outside", ""));
roomPtr hallway(new Room(1, "It's a hallway", ""));
roomPtr kitchen(new Room(2, "It's the kitchen", ""));
roomPtr livingRoom(new Room(3, "You're in the living room", ""));
roomPtr upstairs(new Room(4, "You're on the upstairs landing but all the doors are barred shut", ""));
outside->addOption(optionPtr(new RoomOption(0, "Go inside", hallway)));
hallway->addOption(optionPtr(new RoomOption(0, "Go straight ahead into the kitchen", kitchen)));
hallway->addOption(optionPtr(new RoomOption(1, "Go right into the living room", livingRoom)));
hallway->addOption(optionPtr(new RoomOption(2, "Go upstairs", upstairs)));
hallway->addOption(optionPtr(new RoomOption(3, "Go back outside", outside)));
hallway->addItem(itemPtr(new Item(healthPackID, "Health Pack", "A pack full of first aid items like bandages and surgical spirits. Use it to increase your health", 3)));
kitchen->addOption(optionPtr(new RoomOption(0, "Go right into the living room", livingRoom)));
kitchen->addOption(optionPtr(new RoomOption(1, "Go back into the hallway", hallway)));
kitchen->addItem(itemPtr(new Item(1, "Cake", "A piece of tasty cake", 1)));
kitchen->addItem(itemPtr(new Item(2, "Beer", "A cold bottle of generic brand beer", 1)));
livingRoom->addOption(optionPtr(new RoomOption(0, "Go left into the kitchen", kitchen)));
livingRoom->addOption(optionPtr(new RoomOption(1, "Go back into the hallway",hallway)));
upstairs->addOption(optionPtr(new RoomOption(0, "Go back downstairs", hallway)));
world.addRoom(outside);
world.addRoom(kitchen);
world.addRoom(livingRoom);
world.addRoom(hallway);
world.addRoom(upstairs);
player->setRoom(outside);
}
getOption(selection - 1)
由于添加了两个额外的选项,所以这两个选项不包含在房间的选项向量中,从而导致索引越界错误。在调用getOption
之前,您应该检查选择是否有效。
您应该首先检查selection == inventoryOption
,如果不是(也不是退出选项),则计算optionType
。更好的是,在计算optionType
之前,请确保selection
是一个有效的数字,否则如果用户输入了一个无效的数字,您将遇到同样的问题。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围