![](/img/trans.png)
[英]Segmentation fault when using rapidxml::xml_node<char>::first_attribute
[英]Segmentation Fault when using RapidXML
当我运行它时,它会打印XML加载和维度加载,然后打印Segmentation Fault(核心转储)。 我复制了一个在RapidXML手册中看到的示例,并且在StackOverflow上给出了一个类似的示例。 我的实现有什么问题?
data.xml
<?xml version="1.0"?>
<dimensions>
<x>400</x>
<y>400</y>
<z>1</z>
</dimensions>
地图
#ifndef MAP_H_
#define MAP_H_
class map{
public:
std::vector<int> get_xml();
};
#endif
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;
}
main.cpp
#include <vector>
#include "map.h"
map Map;
int main(int argc, char** argv){
std::vector<int> dims=Map.get_xml();
...
}
看起来您需要调用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.