开发者

"error: stray '\XXX' in C++ program": Why does this happen?

开发者 https://www.devze.com 2023-04-11 12:33 出处:网络
I\'m writing a little program in C++, and come across a strange error: src/Makefile/Tool.cpp:42:3: error: stray ‘\\302’ in program

I'm writing a little program in C++, and come across a strange error:

src/Makefile/Tool.cpp:42:3: error: stray ‘\302’ in program
src/Makefile/Tool.cpp:42:3: error: stray ‘\240’ in program

I'm writing this program in Vim and the corresponding line (showing hidden characters) is:

>--->---std::vector<std::string> { "--debug" }$

This question is not about resolving this er开发者_运维知识库ror, 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!

The question is about what could have caused those errors.


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

You can highlight them with:

:set listchars+=nbsp:.

or any character you like.


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 the "strangers" in the octal flow:

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   /

These two characters in bold are the cause of messages like:

error: stray ‘\302’ in program

You can then remove them, and rebuild.


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.

I deleted the leading white space 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.


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 and iCloud. All I needed to do is apply and replace all using Notepad to all the strange spaces with normal ones and everything compiled normally again.

0

精彩评论

暂无评论...
验证码 换一张
取 消