【问题标题】:Boost property-tree access violation提升属性树访问冲突
【发布时间】:2012-07-04 11:32:32
【问题描述】:

我一直在尝试使用 boost 的属性树来解析 XML 文件,但是每次我想获取字符串的值时,它都会引发访问冲突异常。它适用于整数,所以我有点困惑。 以下是部分代码:

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
    }
}

还有 XML 文件:

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

首先我认为这是因为我没有为字符串分配空间,但 malloc() 和 new char[] 都没有帮助。

任何帮助将不胜感激。在此先感谢:)

【问题讨论】:

  • 你试过pt.get&lt;std::string&gt;("base.char");吗?

标签: c++ xml boost access-violation boost-propertytree


【解决方案1】:

基于this 教程,我认为您需要使用std::string 而不是char* 来获取字符串值。 所以test = pt.get&lt;char*&gt;("base.char"); 的行将是test = pt.get&lt;std::string&gt;("base.char");。 (假设您也将test 的类型更改为std::string)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多