转换为Unicode时出现属性树问题 [英] Boost property tree issue when converting to Unicode

查看:140
本文介绍了转换为Unicode时出现属性树问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,首先,我天生不是C ++开发人员;我已经设法将一些东西放在一起,而且效果很好,但是我敢肯定,通过专家的眼光,它看起来像垃圾=)

Ok, first off I'm not a C++ developer by nature; I've managed to put some stuff together and it works fine, but I'm sure through the eyes of an expert it looks like garbage =)

所以我有一个我制作的免费应用程序,它使用Boost库中的属性树。我使用使用多字节字符集设置开发了整个应用程序(在VS2010中)。我认为是时候进行更新并更新应用程序以支持Unicode了,因为有些人希望更好地支持复杂的字符集。

So I have a freeware app that I've made which uses Property Tree from the Boost libraries. I developed the entire app (in VS2010) with the Use Multi-Byte Character Set setting. I decided it was time to go through and update the app to support Unicode as there are some folks with complex character sets that I'd like to better support.

通过更改所有引用和调用以使用宽字符串以及所有必要的转换的繁琐过程。但是,我完全陷入了困境,我只剩下两个编译器错误。

I went through the tedious process of changing all the references and calls to use wide strings, all the necessary conversions. However, I'm completely stumped at one point, the only two compiler errors I have left.

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

They both come from stream_translator.hpp (/boost/property_tree/), lines 33 and 36 (as noted below):

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行的错误是:

The error at line 33 is:

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行的错误是:

..and the error at line 36 is:

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,此处为默认值)]

From what I've been able to walk backwards through, it's coming from within stream_translator.hpp ultimately beginning as a call to get a value [e.g. ptree.get("some.path", "default value here")]

我真的不知道如何解决此问题,并且似乎找不到任何内容可以帮助我理解问题所在。任何提示或信息都将不胜感激。

I really have absolutely no idea how to resolve this issue and cannot seem to find anything online to help me understand what exactly the problem is. Any tips or info would be greatly appreciated.

编辑

所以我评论了找出与ptree有关的所有内容,直到将其编译为止,然后开始将它们重新加入。事实证明,我可以调用.get很好,它是get_child,在这里出现错误@第36行(尚未完成另一个项目,其中的wstring

So I commented out everything relating to ptree until it would compile, then began adding them back in. It turns out I can call .get fine, it's get_child where the error @ line 36 pops up (haven't done the other project yet, where the wstring issue is).

为简化起见,这是调用的有效顺序,在调用get_child之前是可以的:

To simplify things, here is the effective sequence of the calls, which are fine until get_child is called:

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。

Guessing that your problem was the same I ran into... There are wide character versions of Boost.PropertyTree for unicode support.

对于已安装的Config.xml像这样:

For Config.xml that is setup like this:

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

使用与此类似的代码对其进行解析:

Use code similar to this to parse it:

// 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");
    }
}

这篇关于转换为Unicode时出现属性树问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆