我的朋友正在编写一个基于文本的游戏,并要求我查看这个崩溃的代码。我对其进行了调试,并且在创建动态数组时出现了段错误。我不确定为什么,我建议他完全避免使用指针并使用向量,希望这能解决他的问题,但我很好奇这里到底出了什么问题。这是他的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class nation
{
public:
void init();
string genName();
string getName();
private:
string myName;
int* myBorderPoints;
};
string nation::getName()
{
return myName;
}
string nation::genName()
{
int listLength = 0, listPos = 0, listRand = 0;
string nameToGen = "";
string* namePartList;
ifstream fileName;
fileName.open("NamePart1.txt");
listLength = fileName.tellg();
namePartList = new string[listLength]; // Seg fault here
while (fileName.good())
{
while (!fileName.eof())
{
getline(fileName,namePartList[listPos]);
listPos += 1;
}
}
listRand = rand() % listLength;
nameToGen += namePartList[listRand];
fileName.close();
listLength = 0;
listPos = 0;
listRand = 0;
nameToGen = "";
fileName.open("NamePart2.txt");
listLength = fileName.tellg();
namePartList = new string[listLength];
while (fileName.good())
{
while (!fileName.eof())
{
getline(fileName,namePartList[listPos]);
listPos += 1;
}
}
listRand = rand() % listLength;
nameToGen += namePartList[listRand];
fileName.close();
return nameToGen;
}
void nation::init()
{
srand(time(NULL));
myName = genName();
}
int main()
{
nation testNation;
testNation.init();
cout << testNation.getName();
return 0;
}