使用RapidXML时出现分段错误

时间:2013-11-08 21:38:01

标签: c++ rapidxml

当我运行它时,它会打印加载的XML并加载Dimensions,然后打印Segmentation Fault(核心转储)。我复制了我在RapidXML手册中看到的一个例子,并在StackOverflow上给出了类似的例子。我的实施有什么问题?

data.xml中

<?xml version="1.0"?>
<dimensions>
    <x>400</x>
    <y>400</y>
    <z>1</z>
</dimensions>

map.h

#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();
    ...
}

1 个答案:

答案 0 :(得分:0)

看起来你需要打电话          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;
}