Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:34:19+00:00 2026-06-07T15:34:19+00:00

From what I’ve read this error usually happens when iterators go out of bounds,

  • 0

From what I’ve read this error usually happens when iterators go out of bounds, but I’m getting this problem on an empty vector.

This is the Player class that holds the vector:

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();
}

I commented out every other method in Player that affects inventory. In main, I make a player object, but I don’t do anything to inventory so it should be completely empty, and getInventorySize should return 0, right? But when I do this:

cout<<player->getInventorySize();

The program crashes with a vector subscript out of range error. What’s going on? =\

full main:

#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);


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:34:20+00:00Added an answer on June 7, 2026 at 3:34 pm
    getOption(selection - 1)
    

    Since you are adding two extra options, those two options are not contained within the options vector of the room, causing an index out of bounds error. You should check whether the selection is valid before calling getOption.

    You should first check selection == inventoryOption, and if it is not (and it is not the exit option either), then calculate optionType. Better still, before calculating optionType, make sure that selection is a valid number, otherwise you will get the same problem if a user enters an invalid number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the paper of bigtable. bigtable I read this: Each METADATA row stores approximately
From this article I see I can use msbuild to apply configuration transformations but
From the cpp documentation for std::vector , I see this: void push_back ( const
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement
From my earlier post , I figured out why the command build /nmake USER_C_FLAGS=/DMyVersion=3
For some reason, after submitting a string like this Jack’s Spindle from a text
From PEP 8 : - Imports should usually be on separate lines, e.g.: Yes:
From Project Euler, problem 45: Triangle, pentagonal, and hexagonal numbers are generated by the
From viewing the source code that this code makes it looks like itemValue generate
From the documentation of oracle : Domain Runtime MBean Server : This MBean server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.