C++ 错误:程序中的“\XXX”:为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7663565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: stray '\XXX' in program : Why?
提问by Geoffroy
I'm writing a little program in C++, and come across a strange error :
我正在用 C++ 编写一个小程序,遇到一个奇怪的错误:
src/Makefile/Tool.cpp:42:3: error: stray ‘2' in program
src/Makefile/Tool.cpp:42:3: error: stray ‘0' in program
I'm writing this program in vim and the corresponding line (showing hidden characters) is :
我在 vim 中编写这个程序,相应的行(显示隐藏字符)是:
>--->---std::vector<std::string>?{ "--debug" }$
This question is notabout resolving this error, as I just have to copy back the line and the error-cause disappear.
这个问题不是关于解决这个错误,因为我只需要复制回该行,错误原因就会消失。
It seems that the error is caused by some characters even hidden by vim after activating all relative options!
看来这个错误是由于一些字符在激活所有相关选项后甚至被 vim 隐藏了!
The question isabout what could have caused those errors.
问题是什么可能导致这些错误。
回答by Josh Lee
"\302\240"
is UTF-8 for U+00A0 NO-BREAK SPACE
. Vim won't normally highlight it as anything special, so it's possible for one to sneak in even if you have 'list'
mode enabled.
"\302\240"
是 UTF-8 的U+00A0 NO-BREAK SPACE
。Vim 通常不会将它突出显示为任何特殊的东西,因此即使您'list'
启用了模式,也有可能有人潜入。
You can highlight them with:
您可以使用以下方法突出显示它们:
:set listchars+=nbsp:.
or any character you like.
或任何你喜欢的角色。
回答by Régis KLA
As aforementioned it is due to some not visible characters in your source. One great solution for this is to edit your file in octal mode and you will be able "to see" these characters:
如前所述,这是由于您的来源中有一些不可见的字符。一个很好的解决方案是在八进制模式下编辑您的文件,您将能够“看到”这些字符:
od -c MyClass.hpp
Then you can see in the octal flow the "strangers" :
然后你可以在八进制流中看到“陌生人”:
0001240 t s t r i n g & n a m e )
0001260 { **302 240** t h i s - > n a m e =
0001300 n a m e ; } \n \n \n \t \t \t \t /
This two characters in bold are the cause of messages like
这两个粗体字符是导致消息的原因,例如
error: stray ‘2' in program
You can then remove them, and rebuild.
然后,您可以删除它们,然后重新构建。
Regards.
问候。
回答by Andrew Clemence
For me this problem came from copying my code over from a web browser.
对我来说,这个问题来自从网络浏览器复制我的代码。
I tried doing a :%s/ / /g
in vim
to replace all spaces with real spaces but this has failed.
我尝试做一个:%s/ / /g
invim
用真实空间替换所有空间,但这失败了。
I deleted the leading whitespace of all lines reporting this error and inserted the space characters again. This is labour intensive but appears to be the only solution I could find.
我删除了报告此错误的所有行的前导空格并再次插入空格字符。这是劳动密集型的,但似乎是我能找到的唯一解决方案。
回答by Salem Elnahwy
I had the same issue and it was the character encoding for the spaces before each line. This happened due to copying from notes programs that was synced up with Exchange server & iCloud. All I needed to do is apply a replace all using notepad to all the strange spaces with normal ones and everything compiled normally again.
我有同样的问题,它是每行之前空格的字符编码。这是由于从与 Exchange 服务器和 iCloud 同步的笔记程序复制而发生的。我需要做的就是将所有使用记事本的替换应用于所有带有正常空间的奇怪空间,并再次正常编译所有内容。