json 带有 utf16 字符的字符串无法从 '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>'

我 json 需要测试其中包含 utf16 宽字符的字符串,但我收到以下错误消息:

\..\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);

我以为 wchar_t 在我的 json 中覆盖了 std::wstring。有什么想法吗?你可以在 \u9ce5 中看到我的 utf16 字符。这是本次测试的关键。

我看过这个c2440,但没看到他们在决议中提到的关于 UDT 的内容。

我正在看它前面的这个 which puts an L ,但是使用转义的 c 字符串,我不确定将 L 放在哪里。

std::wstring 不能从像 "my string" 这样的 narrow string literal 初始化,因此编译错误。

您可以从 wide string literal 初始化 std::wstring — 它的语法包括 L 开头引号前的前缀, 例如L"my string"

由于您正在使用字符串文字连接,您需要在所有字符串文字前加上 L 前缀,即:

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