我有一个应该写入下面定义的 gml 文件的类。该类有一种编写方法。如果我调用该函数,当主函数返回时,我会得到一个核心转储。我可以毫无问题地创建类的对象,它只在调用 write 函数时发生。该函数也没有错误地返回并且程序的其余部分运行。
GML 作家:
class GMLWriter {
public:
void write(List<User*> usr, const char* filename);
};
void GMLWriter::write(List<User*> usr, const char* filename)
{
cout << "Filename: " << filename << endl;
ofstream outfile;
outfile.open(filename);
if (!outfile.is_open())
cout << "Couldn't open the file..." << endl;
outfile << "graph [\n";
// Write user data
for (int n = 0; n < usr.size(); n++) {
cout << "Writing node..." << endl;
outfile << "node [\n";
outfile << "id " << usr[n]->getID() << "\n";
outfile << "name \"" << usr[n]->getName() << "\"\n";
outfile << "age " << usr[n]->getAge() << "\n";
outfile << "zip " << usr[n]->getZip() << "\n";
outfile << "]\n";
}
// Write associations
for (int n = 0; n < usr.size(); n++) {
List<int> tList = usr[n]->getFriends();
cout << "Writing edge..." << endl;
//List<int> tempL = usr[n]->getFriends();
for (int i = 0; i < tList.size(); i++) {
outfile << "edge [\n";
outfile << "source " << usr[n]->getID() << "\n";
outfile << "target " << tList[i] << "\n";
outfile << "]\n";
}
}
outfile << "]"; // end graph
cout << "End function" << endl;
outfile.close();
}
用户只包含要写入文件的变量,这些方法工作正常。
我已经在调试器中花了几个小时来解决这个问题,但一直没能找到问题所在。任何帮助将不胜感激。
谢谢!