转换为 Unicode 时提升属性树问题

Boost property tree issue when converting to Unicode

本文关键字:属性 问题 Unicode 转换      更新时间:2023-10-16

好的,首先,我本质上不是C++开发人员;我已经设法将一些东西放在一起并且工作正常,但我敢肯定,通过专家的眼睛,它看起来像垃圾=(

所以我有一个我制作的免费软件应用程序,它使用来自 Boost 库的属性树。我使用"使用多字节字符集"设置开发了整个应用程序(在VS2010中(。我决定是时候通过并更新应用程序以支持 Unicode,因为我想更好地支持一些具有复杂字符集的人。

我经历了将所有引用和调用更改为使用宽字符串的繁琐过程,所有必要的转换。但是,我一度完全被难住了,这是我剩下的仅有的两个编译器错误。

它们都来自 stream_translator.hpp (/boost/property_tree/(,第 33 行和第 36 行(如下所述(:

template <typename Ch, typename Traits, typename E, typename Enabler = void>
struct customize_stream
{
    static void insert(std::basic_ostream<Ch, Traits>& s, const E& e) {
        s << e; //line 33
    }
    static void extract(std::basic_istream<Ch, Traits>& s, E& e) {
        s >> e; //line 36
        if(!s.eof()) {
            s >> std::ws;
        }
    }
};

第 33 行的错误是:

Error   347 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::wstring' (or there is no acceptable conversion)   {...}boost_1_49_0boostproperty_treestream_translator.hpp    33  1   

..第 36 行的错误是:

Error   233 error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion) {...}boost_1_49_0boostproperty_treestream_translator.hpp    36  1

从我能够向后走过的内容来看,它来自 stream_translator.hpp 内部,最终开始于获取值的调用 [例如 ptree.get("some.path", "default value here"(]

真的完全不知道如何解决这个问题,似乎无法在网上找到任何内容来帮助我了解问题到底是什么。 任何提示或信息将不胜感激。

编辑

所以我注释掉了与 ptree 相关的所有内容,直到它编译,然后开始重新添加它们。事实证明,我可以调用.get fine,这是弹出错误@行36的get_child(尚未完成其他项目,wstring问题所在(。

为了简化事情,以下是调用的有效顺序,在调用get_child之前,这些顺序很好:

boost::property_tree::ptree pt; 
boost::property_tree::read_xml("Config.xml", pt);
int iAppSetting = pt.get("config.settings.AppSetting",1); //<- works fine
ptree ptt;
ptt = pt.get_child("config.Applications"); //<- adding this line causes the line 36 error

猜测你的问题和我遇到的一样......有宽字符版本的 Boost.PropertyTree 用于 unicode 支持。

对于配置.xml设置如下:

<?xml version="1.0"?>
<Zoo>
    <Monkey>
        <Food>Bananas</Food>
    </Monkey>
</Zoo>

使用类似于下面的代码来解析它:

// Load up the property tree for wide characters
boost::property_tree::wptree pt;
boost::property_tree::read_xml("Config.xml", pt);
// Iterate
BOOST_FOREACH(wptree::value_type const& v, pt.get_child(L"Zoo"))
{
    if( v.first == L"Monkey" )
    {
        wstring foodType = v.second.get<wstring>(L"Food");
    }
}