Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

text

vim

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 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!

The question is about what could have caused those errors.

like image 974
Geoffroy Avatar asked Oct 05 '11 15:10

Geoffroy


People also ask

What is error stray in C?

Go to the line which is reported and you see the straying special char. That happens if you had opend or edited you sourcecode with an editor that enters “non breakable spaces” instead of the common whitespace. That might happen if you push “shift-space” instead of only “space”, depending on the editor you use.

What does stray error mean?

Error: stray '\240' in program is a character encoding error message. \240 is a non-breaking space in iso8859-1 encoding used on web pages, especially when the code shouldn't get line-wrapped by the browser. The compiler doesn't understand non-breaking space and reports about a straying special character error. eg.


1 Answers

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

like image 94
Josh Lee Avatar answered Sep 25 '22 15:09

Josh Lee