gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:47:21 25 4
gpt4 key购买 nike

我不确定为什么会收到“std::length_error”。下面是我的代码:

#include "受害者.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();
}

这是房间类:

#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”

最佳答案

您的构造函数不会初始化“building”成员。相反,它会创建一个名为“building”的局部变量。

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

关于c++ 抛出 'std::length_error' 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35963845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com