- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定为什么会收到“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/
在我的应用程序中,我还使用了 C++/Boost 库。奇怪的是,所有崩溃日志都与 std::length_error() 有关。 谁能提供一些线索吗?欢迎任何评论。 124 Thread 8 Cras
我的程序由三个文件组成。 arithcard.hpp(头文件)、assign2.cpp(主程序)和arithcard.cpp(方法)。 在数组 cards 中,存储从文本文件读取的字符串。例如,car
这是我得到的错误: terminate called after throwing an instance of 'std::length_error' what(): basic_string::_
我不确定为什么会收到“std::length_error”。下面是我的代码: #include "受害者.hpp" #include "Room.hpp" #include "Item.hpp" #i
我正在尝试用 C++ 编写一个简单的 Hangman 游戏。游戏大部分工作,但有时当我调试它时,我得到异常 std::length_error。通常在用户输入选择后。由于 Stack Overflow
stringstream str(line, ios_base::in); while(!str.eof()){ string word; str >> word; std::
这是我在这里的第一篇文章。由于我是新手,这个问题可能很愚蠢。当显示以下错误消息时,我正在编写一段代码, 在抛出“std::length_error”的实例后调用终止 什么(): basic_strin
因此,我为作业编写了一个程序,内容是使用 IEEE 格式将 float 转换为科学基数 2。当我用程序在我的计算机上运行它时,教授让我们运行的测试通过了,但是当我收到作业时,他说程序在所有测试中都遇到
我收到以下异常。不确定是什么原因造成的。 有什么想法吗? 这是我的连接字符串: std::string brokerURI = "tcp://127.0.0.1:61613?wireFormat=st
这是我的方法之一,哈希有效且已正确分配,但在它退出功能之前,“blockHash”变为“”,这给了我上面的错误。 Block::Block(int index, const double amount
我正在使用 Boost Regex。我有以下代码(简化): bool search_for_match(const string& data) { boost::smatch results;
我用 SFML 开始了一个项目,我想在窗口标题上显示 fps 和勾号,但这不起作用: string title = to_string(frames) + " fps, " + to_string(
当我运行我的程序时,它因以下错误而崩溃: terminate called after throwing an instance of 'std::length_error' what(): basi
我正在 Tutorials Point 上使用 pthread 在 C++ 中学习多线程(它可能很旧,但我只需要一些东西来开始)。我稍微修改了代码: #include #include #incl
当编译器处于发布版本时,我收到标题中提到的运行时错误,而当它处于调试版本时,出现段错误。我做了一些研究,我发现这是因为我将字符串传递给 add 方法的方式。我不清楚,所以如果有人用简单的语言描述为什么
这是我的第一个 c++ 项目,因此可能会有初学者失败。还是要学很多东西。 我正在通过将连接的圆柱体与点云库相匹配来为树木(真正的户外树木)建模。在我的真实场景中,我检测到大约 3000 个圆柱体,我想
我正在尝试使用 Boost.Filesystem 库遍历目录。 问题是当我尝试实例化一个路径对象时,我得到一个 std::length_error 消息“string too long”和任何长度的字
在运行我的程序时出现此错误: terminate called after throwing an instance of 'std::length_error' what(): basic_s
我正在使用 SFML 对于我的项目,我目前正在关注网站上的教程。 目前我只完成了创建 RenderWindow 的第一步 使用以下代码(直接从教程粘贴): #include #include in
在浏览了之前就此主题提出的问题后,我并没有真正理解任何答案。我的助教试图帮助我解决这个问题大约一个小时,但无法解决任何问题。 首先,这是程序编译时出错的地方 在 xthrow.cpp 中 _CRTIM
我是一名优秀的程序员,十分优秀!