0

私が読んだことから、このエラーは通常、反復子が範囲外になったときに発生しますが、空のベクトルでこの問題が発生しています。

これは、ベクトルを保持する 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);


}
4

1 に答える 1

2
getOption(selection - 1)

2 つのオプションを追加しているため、これら 2 つのオプションはルームのオプション ベクトル内に含まれておらず、範囲外のインデックス エラーが発生しています。を呼び出す前に、選択が有効かどうかを確認する必要がありますgetOption

最初に をチェックselection == inventoryOptionし、そうでない場合 (そして終了オプションでもない場合) を計算する必要がありますoptionType。さらに良いことに、 を計算する前に、それが有効な数値であるoptionTypeことを確認してselectionください。そうしないと、ユーザーが無効な数値を入力した場合に同じ問題が発生します。

于 2012-07-14T14:19:02.597 に答える