json string with utf16 char cannot convert from 'const char [566]' to 'std::basic_string<_Elem,_Traits,_Ax>'

被刻印的时光 ゝ 提交于 2019-12-25 09:00:34

问题


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

This is my 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;
    }

This is how it's used

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

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.

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.


回答1:


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

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".

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"
    ...


来源:https://stackoverflow.com/questions/43309439/json-string-with-utf16-char-cannot-convert-from-const-char-566-to-stdbasi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!