下面是加载文件的代码。在按钮单击事件中调用加载函数,该函数返回 XMLElement 指针变量。(返回有效地址)但由于发生分段错误,无法使用 ->operator 访问 XMlElement 的成员。
XMLElement *XMLUtilities::load(string filepath)
{
XMLDocument doc;
char *cstr = new char[filepath.length() + 1];
strcpy(cstr, filepath.c_str());
XMLError err=doc.LoadFile(cstr);
XMLElement *root=nullptr;
if(err==XML_ERROR_FILE_NOT_FOUND)
{
return nullptr;
}
else
{
root=doc.FirstChildElement();
cout<<root->Name();
return root;
}
下面
是按钮点击的代码..
`void MainWindow::on_pushButton_clicked()
{
XMLUtilities util;
QString filepath=QFileDialog::getOpenFileName(this,"open A file","C://");
string str=filepath.toStdString();
XMLElement *doc=util.load(str);
cout<<&doc; **/prints a address location **
cout<<doc->Name(); **/segmentation fault occurs**
if(doc)
{
QMessageBox::information(this,"success",filepath);
// util.traverse(root);
}
else
QMessageBox::information(this,"fail",filepath);
}