C++ cli 为什么会出现错误C3851(通用字符名不能在基本字符集中指定字符)?

C++ cli 为什么会出现错误C3851(通用字符名不能在基本字符集中指定字符)?,c++-cli,removing-whitespace,C++ Cli,Removing Whitespace,我一直在尝试从这里开始翻译,以便在C++-cli项目中运行。就intellisense而言,以下内容看起来不错: static String^ TrimAllWithInplaceCharArray(String^ str) { unsigned int len = str->Length; array<wchar_t, 1>^ src = str->ToCharArray(); int dstIdx = 0; for (unsigned

我一直在尝试从这里开始翻译,以便在C++-cli项目中运行。就intellisense而言,以下内容看起来不错:

static String^ TrimAllWithInplaceCharArray(String^ str)
{
    unsigned int len = str->Length;
    array<wchar_t, 1>^ src = str->ToCharArray();
    int dstIdx = 0;
    for (unsigned int i = 0; i < len; i++) {
        wchar_t ch = src[i];
        switch (ch) {
            case L'\u0020': case L'\u00A0': case L'\u1680': case L'\u2000': case L'\u2001':
            case L'\u2002': case L'\u2003': case L'\u2004': case L'\u2005': case L'\u2006':
            case L'\u2007': case L'\u2008': case L'\u2009': case L'\u200A': case L'\u202F':
            case L'\u205F': case L'\u3000': case L'\u2028': case L'\u2029': case L'\u0009':
            case L'\u000A': case L'\u000B': case L'\u000C': case L'\u000D': case L'\u0085':
                continue;
            default:
                src[dstIdx++] = ch;
                break;
        }
    }
    return gcnew String(src, 0, dstIdx);
}

我是否在某种程度上无法表示数据类型或文字值?

请查看以下方法是否有效

static String^ TrimAllWithInplaceCharArray(String^ str)
{
    unsigned int len = str->Length;
    array<wchar_t, 1>^ src = str->ToCharArray();
    int dstIdx = 0;
    for (unsigned int i = 0; i < len; i++) {
        wchar_t ch = src[i];
        switch (ch) {
        case L'\x20': case L'\xA0': case L'\u1680': case L'\u2000': case L'\u2001':
        case L'\u2002': case L'\u2003': case L'\u2004': case L'\u2005': case L'\u2006':
        case L'\u2007': case L'\u2008': case L'\u2009': case L'\u200A': case L'\u202F':
        case L'\u205F': case L'\u3000': case L'\u2028': case L'\u2029': case L'\x09':
        case L'\x0A': case L'\x0B': case L'\x0C': case L'\x0D': case L'\x85':
            continue;
        default:
            src[dstIdx++] = ch;
            break;
        }
    }
    return gcnew String(src, 0, dstIdx);
}
静态字符串^trimallWithInPlaceCharray(字符串^str)
{
无符号整数len=str->Length;
数组^src=str->ToCharArray();
int-dstIdx=0;
for(无符号整数i=0;i
找到这个

“您不能使用-u0022,因为您不能使用 通用字符名,用于在基本字符表中指定字符 字符集。”


很难猜测,它编译得很干净。当编译器看到的是
L\u0020
而不是
L'\u0020'
时,就会出现这种错误。或者其他看起来像撇号的Unicode字形。因此,它不再将其视为文字。典型副本/顺便说一句。使用十六进制查看器查看源文件。尝试删除“00”怎么样。例如,请使用“\u20”而不是“\u0020”。只是一个简单的猜测。这里有一些关于字符串文字的更有用的背景知识:
static String^ TrimAllWithInplaceCharArray(String^ str)
{
    unsigned int len = str->Length;
    array<wchar_t, 1>^ src = str->ToCharArray();
    int dstIdx = 0;
    for (unsigned int i = 0; i < len; i++) {
        wchar_t ch = src[i];
        switch (ch) {
        case L'\x20': case L'\xA0': case L'\u1680': case L'\u2000': case L'\u2001':
        case L'\u2002': case L'\u2003': case L'\u2004': case L'\u2005': case L'\u2006':
        case L'\u2007': case L'\u2008': case L'\u2009': case L'\u200A': case L'\u202F':
        case L'\u205F': case L'\u3000': case L'\u2028': case L'\u2029': case L'\x09':
        case L'\x0A': case L'\x0B': case L'\x0C': case L'\x0D': case L'\x85':
            continue;
        default:
            src[dstIdx++] = ch;
            break;
        }
    }
    return gcnew String(src, 0, dstIdx);
}