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

标签 c++ visual-studio-2010 winapi boost-propertytree

好的,首先我不是天生的 C++ 开发人员;我已经设法将一些东西放在一起并且工作正常,但我确信从专家的角度来看它看起来像垃圾 =)

所以我制作了一个免费软件应用程序,它使用来自 Boost 库的 Property Tree。我使用“使用多字节字符集”设置开发了整个应用程序(在 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_0\boost\property_tree\stream_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_0\boost\property_tree\stream_translator.hpp    36  1

从我能够向后走的内容来看,它来自 stream_translator.hpp 内部,最终开始作为获取值的调用 [例如ptree.get("some.path", "此处为默认值")]

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

编辑

所以我注释掉了与 ptree 相关的所有内容,直到它可以编译,然后开始将它们重新添加进去。事实证明我可以调用 .get fine,它是 get_child 错误@第 36 行弹出的地方(还没有完成其他项目,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 支持。

对于像这样设置的 Config.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");
    }
}

关于c++ - 转换为 Unicode 时提升属性树问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568531/

相关文章:

c++ - 隐藏元素后调整组框的大小?

c++ - 虚函数编译优化c++

c++ - 获取可取消引用类型的 value_type

delphi - 如何使用Delphi在窗口非客户区使用自定义光标

windows - EXE文件中的IMAGE_FILE_NET_RUN_FROM_SWAP如何影响运行库

c# - 从 C++ 到 C# 的 3D vector 结构

java - TCP 消息中的垃圾 - C++/Java 通信

VCExpress 中的 C++ 程序出现 fatal error LNK1120

c++ - float/double 的小范围随机数,适用于整数类型(C++、VS2010)

c++ - FindFirstFile 在根路径上失败