error: stray '\XXX' in program : Why?

后端 未结 4 980
旧时难觅i
旧时难觅i 2020-12-17 16:47

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         


        
相关标签:
4条回答
  • 2020-12-17 17:28

    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.

    0 讨论(0)
  • 2020-12-17 17:30

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

    0 讨论(0)
  • 2020-12-17 17:33

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

    0 讨论(0)
  • 2020-12-17 17:48

    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 ‘\302’ in program 
    

    You can then remove them, and rebuild.

    Regards.

    0 讨论(0)
提交回复
热议问题