带有utf16 char的json字符串无法从'const char [566]'转换为'std :: basic_string< _Elem,_Traits,_Ax>' [英] json string with utf16 char cannot convert from 'const char [566]' to 'std::basic_string<_Elem,_Traits,_Ax>'

查看:435
本文介绍了带有utf16 char的json字符串无法从'const char [566]'转换为'std :: basic_string< _Elem,_Traits,_Ax>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要测试带有utf16宽字符的字符串的json,但是我收到以下错误消息:

I have json that needs to test a string with a utf16 wide char in it but I get the following error message:

\..\test\TestClass.cpp(617): error C2440: 'initializing' : cannot convert from 'const char [566]' to 'std::basic_string<_Elem,_Traits,_Ax>'


     with
2>          [
2>              _Elem=wchar_t,
2>              _Traits=std::char_traits<wchar_t>,
2>              _Ax=std::allocator<wchar_t>
2>          ]
2>          No constructor could take the source type, or constructor overload resolution was ambiguous

这是我的json:

static std::wstring& BAD_JSON5_missingComma_multipleNewlines_Utf16MixedDosAndUnixLineEndings()
    {
        static std::wstring j =
        "{\n"           <=VS squigly says error on this line
            "\"header\":{\n"
                "\"version\":{\"major\":1,\"minor\":0,\"build\":0}\n"
            "},\n"
            "\"body\":{\n"
                "\"string\":{\"type\":\"OurWideStringClass\",\"value\":\"foo\"},\n\n\n\n"
                "\"int\":[\n"
                    "{\"type\":\"string\",\"value\":\"\\u9CE5\"},\n"
                    "{\"type\":\"Int\",\"value\":5678}\n"
                "],\n"
                "\"double\":{\"type\":\"Double\",\"value\":12.34},\n"
                "\"path1\":[\n"
                    "{\n"
                        "\"string\":{\"type\":\"OurWideStringClass\",\"value\":\"bar\"},\r\n"
                        "\"int\":[\n"
                            "{\"type\":\"Int\"\"value\":7},\n"
                            "{\"type\":\"Int\",\"value\":11}\n"
                        "]\n"
                    "},\n"
                    "{\n"
                        "\"string\":{\"type\":\"OurWideStringClass\",\"value\":\"top\"},\n"
                        "\"int\":[\n"
                            "{\"type\":\"Int\",\"value\":13},\r\n"
                            "{\"type\":\"Int\",\"value\":41}\n"
                        "]\n"
                    "}\n"
                "],\n"
                "\"path2\":{\n"
                    "\"int\":{\"type\":\"Int\",\"value\":-1234},\n"
                    "\"double\":{\"type\":\"Double\",\"value\":-1.234}\r\n"
                "}\n"
            "}\n"
        "}\n"; <=double clicking build error goes to this line

        return j;
    }

这是它的用法

OurWideStringClass slxStJson5 = BAD_JSON5_missingComma_multipleNewlines_Utf16MixedDosAndUnixLineEndings();
std::wistringstream ssJsonMissingCommaUtf16Newlines(slxStJson5);

我以为我的json中的wchar_t被std :: wstring覆盖了.有什么问题吗?您可以在\ u9ce5中看到我的utf16字符.这是此测试的关键.

I thought I had the wchar_t covered with std::wstring in my json. Any ideas what's the issue? you can see my utf16 character in \u9ce5. This is the key to this test.

我看着这个 c2440 ,但看不到它们在有关UDT的决议中指的是什么.

I looked at this c2440 but don't see what they are referring to in the resolution with regard to UDT.

我正在查看

I was looking at this which puts an L in front of it, but with escaped c string, I'm not sure where to put the L.

推荐答案

std::wstring无法通过"my string"这样的窄字符串常量,因此编译错误.

std::wstring cannot be initialized from a narrow string literal like "my string", hence the compilation error.

您可以通过宽字符串文字初始化std::wstring —它的语法在开头的引号之前包含 L前缀,例如L"my string".

You can initialize a std::wstring from a wide string literal — its syntax includes the L prefix before the opening quote, e.g. L"my string".

在使用字符串文字串联时,您需要在所有字符串文字前加上L 前缀,即:

As you are using string literal concatenation, you need to prefix all the string literals with L, i.e.:

static std::wstring j =
    L"{\n"
        L"\"header\":{\n"
            L"\"version\":{\"major\":1,\"minor\":0,\"build\":0}\n"
        L"},\n"
    ...

这篇关于带有utf16 char的json字符串无法从'const char [566]'转换为'std :: basic_string&lt; _Elem,_Traits,_Ax&gt;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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