Frage

I have been trying to parse an XML file using boost's property tree, but every time I want to get the value of a string it throws an access violation exception. It works fine with integers so I'm a bit confused. Here's some of the code:

class Config
{
    char * test;
    int test2;

public:
    Config();
};

Config::Config(void)
{
    boost::property_tree::ptree pt;
    boost::property_tree::xml_parser::read_xml("config.xml", pt);

    try
    {
        test = pt.get<char*>("base.char");
        test2 = pt.get<int>("base.int");
    }
    catch(std::exception e)
    {
        //something wasn't specified
    }
}

And the XML file:

<base>
    <char>test</char>
    <int>10</int>
</base>

First I thought it's because I didn't allocate space for the string but neither malloc() nor new char[] helped.

Any help would be appreciated. Thanks in advance :)

War es hilfreich?

Lösung

Based on this tutorial I think you need to use std::string instead of char* to get string values. So the line test = pt.get<char*>("base.char"); would then be test = pt.get<std::string>("base.char");. (Assuming you change test's type to std::string as well).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top