13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • Why don't you post the real code? – Nawaz Oct 05 '11 at 15:14
  • It is the real code. You want the whole scope? I didn't though it was useful, but if needed I can. – Jaffa Oct 05 '11 at 15:15
  • What? The real code is containing `>--->---`? and `$` at the end? – Nawaz Oct 05 '11 at 15:16
  • 1
    @Nawaz: Those are vim list characters (visible whitespace). – Cat Plus Plus Oct 05 '11 at 15:17
  • @CatPlusPlus: Weird. With those characters,how can anyone say anything about the real problem? – Nawaz Oct 05 '11 at 15:18
  • 5
    @Nawaz: They're colourable. Visible white space is a good thing. – Cat Plus Plus Oct 05 '11 at 15:19
  • 1
    This particular stray character has been observed by copy pasting code through Skype Chat. – Peter Mortensen Mar 06 '21 at 13:34
  • How did the [Option + Space experiment](https://stackoverflow.com/questions/7663565/error-stray-xxx-in-c-program-why-does-this-happen/18971009#comment9313293_7663664) turn out? – Peter Mortensen Mar 06 '21 at 14:04
  • In a different context ([PowerShell](https://en.wikipedia.org/wiki/Windows_PowerShell) scripts on Windows), the same character will result in: *"Â : The term 'Â' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. ... CategoryInfo : ObjectNotFound: (Â:String) [], CommandNotFoundException ... FullyQualifiedErrorId : CommandNotFoundException"* – Peter Mortensen Mar 06 '21 at 14:12
  • 1
    Does this answer your question? [Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc) – Karl Knechtel Jan 16 '23 at 14:36

4 Answers4

23

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

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • Thank you, but why is this character in my code? I just edit it with vim and nothing else, so I don't know where may it come from :/ – Jaffa Oct 05 '11 at 15:22
  • 4
    @Geoffroy are you sure you didn't copy-paste any code from a webpage or a manpage or something? – hobbs Oct 05 '11 at 15:25
  • @Josh Lee thanks for the settings, it was missing :) But any idea why is it in my code? – Jaffa Oct 05 '11 at 15:32
  • 1
    @Geoffroy, what is your keyboard layout? Mine is BÉPO and I can easily insert NBSPs with Shift-space. – Benoit Oct 05 '11 at 15:42
  • Default Mac OS French, this character is not easily available – Jaffa Oct 05 '11 at 15:49
  • One additional comment: if you have doubts about a space character (or any character) in `vim`, position the cursor on it and enter `ga`. This will display the encoding in the control line. – James Kanze Oct 05 '11 at 15:51
  • @Geoffroy Are you sure that Option+Space won't do it? – James Kanze Oct 05 '11 at 15:54
  • Good question, I never tested. When back home I check this out, thanks – Jaffa Oct 05 '11 at 16:09
  • In case someone almost misses @xoolive's comment like I did, you can replace these in Vim with `:%s/\%o302//g` and you might also need `:%s/\%o240//g`. – ordonezalex Apr 20 '16 at 01:19
  • 302 and 240 are octal numbers (decimal 194 160. Hexadecimal 0xC2 0xA0). – Peter Mortensen Mar 06 '21 at 19:17
10

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Régis KLA
  • 101
  • 1
  • 4
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    If the stray is `\302`, you can use `:% s,\%o302,,g` – xoolive Nov 17 '15 at 09:27
  • There isn't any need to it manually. It can probably be done by [regular expression search/replace for the Unicode code point](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc/54352836#54352836) - `\x{00A0}`. I would surprised if Vim couldn't do it. It works fine in Notepad++ (Windows) and Geany (Linux). – Peter Mortensen Mar 06 '21 at 13:31
  • Note: The notation is different in [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code) (and probably others): `\u00A0` (instead of `\x{00A0}`) – Peter Mortensen Apr 30 '23 at 02:40
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131