C++ 使用RapidXML时出现分段错误
当我运行这个程序时,它会打印加载的XML和加载的维度,然后打印分段错误(核心转储)。我复制了我在RapidXML手册中看到的一个示例,在StackOverflow上给出了一个类似的示例。我的实现有什么问题 data.xmlC++ 使用RapidXML时出现分段错误,c++,rapidxml,C++,Rapidxml,当我运行这个程序时,它会打印加载的XML和加载的维度,然后打印分段错误(核心转储)。我复制了我在RapidXML手册中看到的一个示例,在StackOverflow上给出了一个类似的示例。我的实现有什么问题 data.xml <?xml version="1.0"?> <dimensions> <x>400</x> <y>400</y> <z>1</z> </dimensi
<?xml version="1.0"?>
<dimensions>
<x>400</x>
<y>400</y>
<z>1</z>
</dimensions>
400
400
1.
地图.h
\ifndef映射_
#定义映射_
类图{
公众:
std::vector get_xml();
};
#恩迪夫
map.cpp
#include <vector>
#include <iostream>
#include "../lib/rapidxml.hpp"
#include "../lib/rapidxml_print.hpp"
#include "../lib/rapidxml_utils.hpp"
using std::vector;
using std::cout;
using std::endl;
using namespace rapidxml;
vector<int> map::get_xml(){
file<> xmlFile("res/data.xml");
cout<<"XML loaded"<<endl;
xml_document<> doc;
xml_node<> *dims=doc.first_node("dimensions");
vector<int> values;
cout<<"Dimensions loaded"<<endl;
for(xml_node<> *dim=dims->first_node();dim;dim->next_sibling()){//breaks here
cout<<"Looping through attributes"<<endl;
values.push_back(atoi(dim->value()));
}
cout<<"All values loaded"<<endl;
return values;
}
#包括
#包括
#包括“./lib/rapidxml.hpp”
#包括“./lib/rapidxml_print.hpp”
#包括“./lib/rapidxml_utils.hpp”
使用std::vector;
使用std::cout;
使用std::endl;
使用名称空间rapidxml;
向量映射::get_xml(){
文件xmlFile(“res/data.xml”);
cout看起来你需要打电话
doc.parse();
根据你的意见
试试这个:
vector<int> map::get_xml(){
file<> xmlFile("res/data.xml");
cout<<"XML loaded"<<endl;
xml_document<> doc;
doc.parse<0>(xmlFile.data());
xml_node<> *dims=doc.first_node("dimensions");
vector<int> values;
cout<<"Dimensions loaded"<<endl;
for(xml_node<> *dim=dims->first_node();dim;dim->next_sibling()){//breaks here
cout<<"Looping through attributes"<<endl;
values.push_back(atoi(dim->value()));
}
cout<<"All values loaded"<<endl;
return values;
}
向量映射::get_xml(){
文件xmlFile(“res/data.xml”);
你确定dims
不是空的吗?分段错误到底发生在哪里?如果你在使用调试器,直到可以执行哪一行?我正在使用g++进行编译。但是从我的cout来看,它必须是带有for循环参数的东西。我测试了它,dims是空的。我如何解决这个问题
#include <vector>
#include "map.h"
map Map;
int main(int argc, char** argv){
std::vector<int> dims=Map.get_xml();
...
}
vector<int> map::get_xml(){
file<> xmlFile("res/data.xml");
cout<<"XML loaded"<<endl;
xml_document<> doc;
doc.parse<0>(xmlFile.data());
xml_node<> *dims=doc.first_node("dimensions");
vector<int> values;
cout<<"Dimensions loaded"<<endl;
for(xml_node<> *dim=dims->first_node();dim;dim->next_sibling()){//breaks here
cout<<"Looping through attributes"<<endl;
values.push_back(atoi(dim->value()));
}
cout<<"All values loaded"<<endl;
return values;
}