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,030
  • 21
  • 100
  • 124
Geoffroy
  • 12,113
  • 4
  • 47
  • 97
  • 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. – Geoffroy 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
  • This particular stray character has been observed by copy pasting code through Skype Chat. – Peter Mortensen Mar 06 '21 at 13:34
  • Why did you accept the answer when it did not even come close to answering the question (***what could have caused*** the error)? – Peter Mortensen Mar 06 '21 at 14:02
  • 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

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
  • 161,055
  • 37
  • 262
  • 269
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,030
  • 21
  • 100
  • 124
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,030
  • 21
  • 100
  • 124
  • 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
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,030
  • 21
  • 100
  • 124