MFD55012
MFD55012

Reputation: 13

c++ throwing instance of 'std::length_error'

I am not sure why I am getting the 'std::length_error'. Below is my code:

#include "Victim.hpp"

#include "Room.hpp"
#include "Item.hpp"
#include "Die.hpp"
#include "Game.hpp"
#include "Firefighter.hpp"

#include <iostream>
#include <string>

int main()
{
    Game game1 = Game();
    std::cout << "Game made" << std::endl;

    std::cout << game1.getPlayerLocation() << std::endl;

    Room *tempRoom = new Hallway();
    std::cout << tempRoom->getType() << std::endl;

    std::string name = game1.getCurrentRoomName();
    return 0;
 }

When it runs the first two line, it works as expected. When it reaches the third line, I get the error. I'd appreciate any help. I've seen that it could be that I'm not really returning a string, but I don't see how I wouldn't be.

Header file for my game:

#ifndef GAME_HPP
#define GAME_HPP

#include "Victim.hpp"
#include "Room.hpp"
#include "Item.hpp"
#include "Firefighter.hpp"
#include <string>

class Game
{
    protected:
        int NUM_ROOMS;
        //Room* building1[];
        Room* building;
        Victim* oldVic;
        Victim* kidVic;
        Victim* pupVic;
        Die roomSelect;
        Die itemDie;
        Die victimPlace;
        int victimLocation[];                   //where the victims will be     in the building
        int sprinklerLocation;
    int closetLocation;
    int itemSelect;
    Item *closetItem;

    int moves;
    Firefighter player1;

    int playerLocation;

public:
    Game();

    //void makeBuilding();
    void setPlayerLocation(int);
    void move();
    int getPlayerLocation();
    std::string getCurrentRoomName();
};

#endif

This is the game Class:

#include "Game.hpp"
#include "Victim.hpp"
#include "Room.hpp"
#include "Item.hpp"
#include "Firefighter.hpp"

#include <iostream>
#include <string>

Game::Game() : roomSelect(10), victimPlace(9), itemDie(7), player1()
{
    int NUM_ROOMS = 10;
    //Room* building1[NUM_ROOMS];
    Room* building = new Hallway();

    Victim* oldVic = new Adult();
    Victim* kidVic = new Child();
    Victim* pupVic = new Puppy();

    int victimLocation[] = {0,0,0};                 //where the victims will be in the building
    int sprinklerLocation = 0;
    int closetLocation = 0;
    int itemSelect = 0;
    int moves = 40;
    Item *closetItem;
    int numSaved = 0;

    setPlayerLocation(0);

    //makeBuilding();
}

void Game::setPlayerLocation(int spot)
{
    playerLocation = spot;
}

void Game::move()
{
    int startSpot = playerLocation;

    while(playerLocation == startSpot)
    {
        int choice;
        std::cout << "Enter the number for which way you would like to go:" << std::endl;
        std::cout << "1: Left" << std::endl;
        std::cout << "2: Right" << std::endl;
        std::cin >> choice;

        if(choice == 1 && playerLocation == 0)
            std::cout << "You cannot move to the left" << std::endl;
        else if(choice == 2 && playerLocation == NUM_ROOMS - 1)
            std::cout << "You cannot move to the right" << std::endl;
        else if(choice == 1 && playerLocation > 0)
            playerLocation -= 1;
        else if(choice == 2 && playerLocation < NUM_ROOMS - 1)
            playerLocation += 1;
    }
}

int Game::getPlayerLocation()
{
    return playerLocation;
}

std::string Game::getCurrentRoomName()
{
    return building->getType();
}

This is the Room class:

#include "Room.hpp"
#include "Item.hpp"
#include "Victim.hpp"
#include <string>

Room::Room()
{
    onFire = false;
    broken = false;
    searched = false;
    type = "Room";
    vic = NULL;
    left = NULL;
    right = NULL;
    upstairs = NULL;
    downstairs = NULL;
}

/* Room::Room(bool fire, Victim *vick, Room *l, Room *r, Room *u, Room *d)
{
    onFire = fire;
    broken = false;
    searched = false;
    vic = vick;
    left = l;
    right = r;
    upstairs = u;
    downstairs = d;
} */

Room::~Room()
{

}

void Room::setVictim(Victim *vick)
{
    vic = vick;
}

/* void Room::setLeft(Room *l)
{
    left = l;
}

void Room::setRight(Room *r)
{
    right = r;
}

void Room::setUpstairs(Room *u)
{
    upstairs = u;
}

void Room::setDownstairs(Room *d)
{
    downstairs = d;
} */

void Room::setBroken(bool george)
{
    broken = george;
}

void Room::setSearched(bool george)
{
    searched = george;
}

/* Room* Room::getLeft()
{
    return left;
}

Room* Room::getRight()
{
    return right;
}

Room* Room::getUpstairs()
{
    return upstairs;
}

Room* Room::getDownstairs()
{
    return downstairs;
} */

bool Room::getOnFire()
{
    return onFire;
}

bool Room::getBroken()
{
    return broken;
}

Victim* Room::getVictim()
{
    return vic;
}

std::string Room::getType()
{
    return type;
}

/* Entrance::Entrance()
{
    onFire = false;
    broken = false;
    searched = false;
    type = " ";
    vic = NULL;
    left = NULL;
    right = NULL;
    upstairs = NULL;
    downstairs = NULL;
}

Entrance::Entrance(bool fire, Victim *vick, Room *l, Room *r, Room *u, Room *d)
{
    onFire = fire;
    broken = false;
    searched = false;
    vic = vick;
    left = l;
    right = r;
    upstairs = u;
    downstairs = d;
}

void Entrance::setOnFire()
{
    onFire = true;
    vic->passedOut();
}

Stairway::Stairway()
{
    onFire = false;
    broken = false;
    searched = false;
    type = " ";
    vic = NULL;
    left = NULL;
    right = NULL;
    upstairs = NULL;
    downstairs = NULL;
}

Stairway::Stairway(bool fire, Victim *vick, Room *l, Room *r, Room *u, Room *d)
{
    onFire = fire;
    broken = false;
    searched = false;
    vic = vick;
    left = l;
    right = r;
    upstairs = u;
    downstairs = d;
}

void Stairway::setOnFire()
{
    onFire = true;
    broken = true;
}

SupplyCloset::SupplyCloset()
{
    onFire = false;
    broken = false;
    searched = false;
    type = " ";
    vic = NULL;
    left = NULL;
    right = NULL;
    upstairs = NULL;
    downstairs = NULL;
    item1 = NULL;
}

SupplyCloset::SupplyCloset(bool fire, Victim *vick, Room *l, Room *r, Room *u, Room *d, Item *i)
{
    onFire = fire;
    broken = false;
    searched = false;
    vic = vick;
    left = l;
    right = r;
    upstairs = u;
    downstairs = d;
    item1 = i;
}

void SupplyCloset::setItem(Item *i)
{
    item1 = i;
}

Item* SupplyCloset::getItem()
{
    return item1;
}

void SupplyCloset::setOnFire()
{
    onFire = true;
    setItem(NULL);
}

SprinklerControl::SprinklerControl()
{
    onFire = false;
    broken = false;
    searched = false;
    type = " ";
    vic = NULL;
    left = NULL;
    right = NULL;
    upstairs = NULL;
    downstairs = NULL;
    sprinklerSwitch = false;
}

SprinklerControl::SprinklerControl(bool fire, Victim *vick, Room *l, Room *r, Room *u, Room *d)
{
    onFire = fire;
    broken = false;
    searched = false;
    vic = vick;
    left = l;
    right = r;
    upstairs = u;
    downstairs = d;
    sprinklerSwitch = false;
}

bool SprinklerControl::getSwitch()
{
    return sprinklerSwitch;
}

void SprinklerControl::hitSwitch()
{
    if(sprinklerSwitch == true)
        sprinklerSwitch = false;
    else
        sprinklerSwitch = true;
}

void SprinklerControl::setOnFire()
{
    onFire = true;
    broken = true;
} */

Hallway::Hallway()
{
    onFire = false;
    broken = false;
    searched = false;
    type = "Hallway";
    vic = NULL;
    left = NULL;
    right = NULL;
    upstairs = NULL;
    downstairs = NULL;
}

/* Hallway::Hallway(bool fire, Victim *vick, Room *l, Room *r, Room *u, Room *d)
{
    onFire = fire;
    broken = false;
    searched = false;
    vic = vick;
    left = l;
    right = r;
    upstairs = u;
    downstairs = d;
} */

void Hallway::setOnFire()
{
    onFire = true;
}

where type is just a string that states the type of room, like "Hallway"

Upvotes: 0

Views: 762

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118310

Your constructor does not initialize the 'building' member. It creates a local variable named 'building', instead.

As such, dereferencing the 'building' member variable results in undefined behavior. Did you try using a debugger to step through the code, which would've made this bug quite apparent?

Upvotes: 1

Related Questions