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

C++ 尝试以随机字符打印字符串返回结果(长读取),c++,function,reference,return,C++,Function,Reference,Return,这可能需要一点时间来解释 我正在为编程课制作一个基于文本的游戏。我试图根据玩家的输入描述玩家当前所处的环境。 第一步: if (playerChoice == 3) { //If they choose option 3, run the describe environment function bool restart = CheckSurroundings(PlayerCharacter, ItemList, Map); } 下一步: static bool CheckSurro

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

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

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;
}
编辑:如果有人问,我确实设置了一个构造函数,其中的描述以字符串“a”给出

既然已经给出了所有需要给出的上下文,我可以从上面跳回到cout语句:

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

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

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

商场入口走廊 商场的入口被各种长凳、垃圾桶、空的自动售货机和偶尔的木板堵住了。这不会让僵尸在外面呆太久

所以它是有效的,代码如下。损坏的部分不在您向我们展示的内容中,或者您的编译器或环境存在问题,我认为这是极不可能的,但现在您可以复制下面的内容并自行测试。我对你没有展示给我们的部分做了一些非常简单的假设,所以你必须分享这些假设,或者找出你的结果不同的原因。我的第一个建议是将实际的Player类和Position()函数替换为我的。我想知道您对职位的调用是否会以某种方式修改内部成员的状态,以便第一次调用(对于名称)有效,但第二次调用无效。只是一个想法

#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;
}
#包括
#包括
使用名称空间std;
班级面积{
私人:
std::字符串区域名;
std::字符串区域描述;
公众:
std::string Name(){//GetName
返回域名;
}
void Name(std::string newName){//SetName
areaName=新名称;
}
//区域描述
std::string Description(){//GetDescription
返回区域描述;
}
void描述(std::string newDesc){//SetDescription
areaDescription=newDesc;
}
};
静态void SetupMapID(矢量和贴图,int-mapSize){
地图[0]。名称(“商场入口走廊”);
地图[0]。描述(“商场的入口被各种长凳、垃圾桶、空自动售货机和偶尔的木板堵住了。这不会让僵尸在外面呆太久。”);
}
类播放器{
公众:
int位置(){
返回0;
}    
};
int main()
{    
int mapSize=17;
矢量地图(mapSize);
SetupMapID(地图、地图大小);
玩家角色;

“这可能是解决问题的最佳时机。@某个程序员,嗯,我想这是我的帖子。我写这篇文章感觉很糟糕,但我真的不知道如何解决我的问题。我花了两个小时试图调试这个问题,但没有找到解决方案。只是一个猜测,但我敢打赌这是一个边界问题。如果你替换
Map会怎么样[PlayerCharacter.Position()]
with
Map.at(PlayerCharacter.Position())
我没有看到你调整过向量的大小,因此看起来你的所有访问都超出了范围。你的示例不完整。没有人会尝试将你的小片段拼凑成一个完整的程序,如果他们这样做了,很难确定它是否与你的相同。将你的示例压缩成可以在一个c中发布的内容ode块演示问题。如下所示:如果在缩小示例时发现问题消失了,那么您就缩小了问题的范围。使用这些知识构建示例,或者您将看到如何自己解决问题。学习调试程序是一项重要的学习技能。
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;
        }
};
cout << Map[PlayerCharacter.Position()].Description() << endl;
#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;
}