我有這樣一個簡單的C++代碼:C++展示奇怪的行爲
#include <iostream>
#include <string>
using namespace std;
int main() {
int n , a , b ;
cin >> n >> a >> b ;
//This two lines are exactly same!!!!
cout << n - max(a + 1, n - b) + 1 << endl ;
//cout << n - max(a + 1, n - b) + 1 << endl ;
}
在這段代碼中,我有兩個線是完全一樣的,但是當我編譯確切上面的代碼,我得到我的結果與任何輸入例如(5,2,3),但是當我取消對第二cout
和評論的第一個與所有以前的狀態代碼不編譯(GNU 4.8.2),並得到這個錯誤:
sample.cpp:8:5: error: stray ‘\342’ in program
cout << n - max(a + 1, n - b) + 1 << endl ;
^
sample.cpp:8:5: error: stray ‘\200’ in program
sample.cpp:8:5: error: stray ‘\211’ in program
sample.cpp:8:5: error: stray ‘\342’ in program
sample.cpp:8:5: error: stray ‘\200’ in program
sample.cpp:8:5: error: stray ‘\211’ in program
sample.cpp:8:5: error: stray ‘\342’ in program
sample.cpp:8:5: error: stray ‘\200’ in program
sample.cpp:8:5: error: stray ‘\211’ in program
sample.cpp:8:5: error: stray ‘\342’ in program
sample.cpp:8:5: error: stray ‘\200’ in program
我嘗試與Microsoft Visual C++ 2010編譯器,我得到了相同的結果,但不同的錯誤:
program.cpp
program.cpp(9) : error C2065: 'n¢?%' : undeclared identifier
program.cpp(9) : error C2065: 'a¢?%' : undeclared identifier
program.cpp(9) : error C2065: '¢?%1' : undeclared identifier
program.cpp(9) : error C2065: '¢?%n¢?%' : undeclared identifier
program.cpp(9) : error C2065: '¢?%b' : undeclared identifier
program.cpp(9) : error C2146: syntax error : missing ';' before identifier '¢?%'
program.cpp(9) : error C3861: '¢?%max': identifier not found
program.cpp(9) : error C2065: '¢?%' : undeclared identifier
program.cpp(9) : error C2065: '¢?%1' : undeclared identifier
我無法理解這兩個cout
是完全一樣的,但第一個正常工作和第二個不爲什麼這是一個編譯器錯誤?
考慮使用不會插入怪異字符不同的文本編輯器。我衷心建議VIM – 2014-10-04 12:13:07