c ++抛出'std :: length_error'的实例

时间:2016-03-12 22:00:34

标签: c++ c++11

我不知道为什么我得到'std :: length_error'。以下是我的代码:

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

当它运行前两行时,它按预期工作。当它到达第三行时,我得到错误。我很感激任何帮助。我已经看到可能是因为我没有真正返回一个字符串,但我不知道我怎么会不会。

我的游戏的头文件:

#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

这是游戏类:

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

这是Room类:

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

其中type只是一个表示房间类型的字符串,如“Hallway”

1 个答案:

答案 0 :(得分:1)

您的构造函数未初始化“building”成员。它创建了一个名为“building”的局部变量。

因此,取消引用“building”成员变量会导致未定义的行为。您是否尝试使用调试器来逐步执行代码,这会使此错误变得非常明显?