尝试以随机字符(长读取)打印字符串返回结果



这可能需要一点时间来解释。

我正在为编程课制作一个基于文本的游戏。我试图根据玩家的输入来描述玩家当前所处的环境。 第一步:

if (playerChoice == 3) { //If they choose option 3, run the describe environment function
bool restart = CheckSurroundings(PlayerCharacter, ItemList, Map);
}

下一步:

static bool CheckSurroundings(Player &PlayerCharacter, vector <Item> &ItemList, vector<Area> &Map) {
cout << Map[PlayerCharacter.Position()].GetDescription() << endl;
//cut unimportant stuff out
return false;
}

上面的内容目前在我的台式计算机上输出一个笑脸,并在我的笔记本电脑上运行时打印一个完全不同的字符。

"地图"是区域对象的向量:

int mapSize = 17;
vector <Area> Map (mapSize);
SetupMapID(Map, itemList, mapSize);

包含函数 SetupMapID 很重要。在这里,我通过为矢量内的所有区域对象提供名称、描述和其他属性来"设置"地图矢量。这是它的外观预览:

static void SetupMapID(vector<Area> &Map, vector <Item> ItemList, int mapSize) {
Map[0].Name("The Mall Entrance Hallway");
Map[0].Description("The entrance to the mall has been blockaded with various benches, trash cans empty vending machines, and the occaisonal wood plank. This won't keep the zombies out for long.");
Map[0].Accessible(true);
}

这是实际的区域类头文件(或者,其中一些,删除了不重要的内容)

class Area {
private:
std::string areaName;
std::string areaDescription;
public:
std::string Name() { //GetName
return areaName;
}
void Name(std::string newName) { //SetName
areaName = newName;
}
//Area Description
std::string Description() { //GetDescription
return areaDescription;
}
void Description(std::string newDesc) { //SetDescription
areaDescription = newDesc;
}
};

编辑:如果有人问,我确实设置了一个构造函数,其中描述给出了"A"字符串。

现在所有需要给出的上下文都已经给出,我可以跳回到上面的 cout 语句:

cout << Map[PlayerCharacter.Position()].Description() << endl;

地图,正如你所看到的,是区域对象的矢量,我希望抓住玩家当前所在的区域对象,可以使用PlayerCharacter.Position()检索(这返回0 - 15)。所以"Map[PlayerCharacter.Position()]"得到那个Area对象。然后,在那之后。不带参数的函数 Description() 返回对象的描述字符串。没有什么特别复杂的,但由于我不知道的原因,它没有返回它应该返回的字符串("商场入口已被封锁......"),而是返回一个随机的 ASCII 字符(我认为 ASCII?在我的台式计算机上,它总是返回一个笑脸。

这就是我卡住的地方。为什么会返回笑脸?Name() 函数按预期运行(以字符串形式返回区域的"名称"),与 THAT 相关的所有内容在风格上都完全相同。

请帮忙!我可以通过创建一个字符串数组并将 Area 的 # 扔给它来解决它,但我最愿意避免这种情况。

我想知道问题是否与空间不足有关?我的导师认为这可能与使用引用变量有关,但我完全不知道为什么 Name() 有效,而 Description() 不起作用。

我花了 5 分钟将您的片段(通过复制和粘贴)组装成实际的可编译和可执行的代码片段。

如果我在 Linux 上使用 gcc 编译以下内容,我会得到以下输出(直接从控制台窗口复制和粘贴):

商场入口走廊 商场的入口已被各种长凳、垃圾桶空自动售货机和偶尔的木板封锁。这不会让僵尸长时间呆在外面。

所以它有效,代码如下。损坏的部分不在您向我们展示的内容中,或者您的编译器或环境存在问题,我认为这极不可能,但现在您可以复制以下内容并自己测试。我对你没有向我们展示的部分做了一些极其简化的假设,所以你将不得不分享这些假设或弄清楚为什么你的结果是不同的。我的第一个建议是把你实际的玩家类和 Position() 函数代替我的。我想知道您对 Position 的调用是否以某种方式修改了内部成员的状态,因此第一次调用(对于 Name)有效,但第二次调用无效。只是一个想法。

#include <iostream>
#include <vector>
using namespace std;
class Area {
private:
std::string areaName;
std::string areaDescription;
public:
std::string Name() { //GetName
return areaName;
}
void Name(std::string newName) { //SetName
areaName = newName;
}
//Area Description
std::string Description() { //GetDescription
return areaDescription;
}
void Description(std::string newDesc) { //SetDescription
areaDescription = newDesc;
}
};
static void SetupMapID(vector<Area> &Map, int mapSize) {
Map[0].Name("The Mall Entrance Hallway");
Map[0].Description("The entrance to the mall has been blockaded with various benches, trash cans empty vending machines, and the occaisonal wood plank. This won't keep the zombies out for long.");
}
class Player {    
public:
int Position() {
return 0;
}    
};

int main()
{    
int mapSize = 17;
vector <Area> Map (mapSize);
SetupMapID(Map, mapSize);
Player PlayerCharacter;
cout << Map[PlayerCharacter.Position()].Name() << endl;
cout << Map[PlayerCharacter.Position()].Description() << endl;
return 0;
}

最新更新