Stray '\\342' in C++ program

2019-01-20 08:36发布

问题:

I'm getting these errors in my program after pasting in some code:

showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:66: error: stray ‘\’ in program
showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program

Here are the two lines that are causing the errors.

size_t startpos = str.find_first_not_of(” \t”); 
size_t endpos = str.find_last_not_of(” \t”); 

How to fix this?

回答1:

The symbol is not ". Those are called 'smart quotes' and are usually found in rich documents or blogs.



回答2:

The lines

 size_t startpos = str.find_first_not_of(” \t”); 
 size_t endpos = str.find_last_not_of(” \t”); 

have some "special" kind of double quotes, try the following:

 size_t startpos = str.find_first_not_of(" \t"); 
 size_t endpos = str.find_last_not_of(" \t"); 


回答3:

You can use the sed command to fix these issues.

This will give you a quick preview of what will be replaced.

sed s/[”“]/'"'/g File.txt

This will do the replacements and put the replacement in a new file called WithoutSmartQuotes.txt.

sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt

This will overwrite the original file.

sed -i ".bk" s/[”“]/'"'/g File.txt



回答4:

It is worth mentioning here (for whoever lands on this page just like me) that this sort of error message error: stray ‘\xyz’ in program can appear with any other character or symbol that is not recognized by the compiler as a legal one.

Sharing my personal experience:

 - bool less<const char∗>(const char∗ a, const char∗ b)  
 - bool less<const char*>(const char* a, const char* b)   

Former one is copy-pasted from a PDF file. It doesn't compile..

Later one compiles as expected.