我在Mac上學習了C++,並且最近轉移到了Windows 7上。我下載了windows v7.1 sdk並運行了安裝程序。它的.net 4依賴版本的sdk,並且我安裝了.net 4。編譯器說邏輯,並且是未定義的標識符
我正在使用命令行,因爲我更喜歡使用它,我使用gcc編譯器在Mac上做了這個,因爲我對編程非常陌生,所以我非常善於使用它。
我一直在使用v7.1 sdk開發人員命令提示符,因爲它使用SetEnv批處理文件設置環境變量。
編譯器顯然是來自Microsoft的cl.exe編譯器。
我運行了典型的非常簡單的hello world程序,其中包括一個getchar()結束讓我真正看到程序,因爲mac不需要這個新東西。而且getchar工作得很好,程序編譯並運行良好。
當我嘗試編譯我在Mac上編寫的一些源代碼時,出現問題。順便說一句,在Mac上編譯好了。它開始拋出一些非常奇怪的錯誤,比如告訴我邏輯'和'操作符是一個未定義的標識符。現在我可能在這裏是一個愚蠢的人,但從我的理解來看,和運算符不是一個標識符,它是一個運算符。
因此,我決定通過編寫一個非常簡單的程序來縮小問題範圍,該程序使用一個if語句和一個else語句以及'and'運算符,並查看會發生什麼。下面是我試圖編譯代碼:
//hello, this is a test
#include <iostream>
int main()
{
char end;
int a = 0, b = 0;
std::cout << "If the variable a is larger than 10 and variable b is less than a, then b will be subtracted from a, else they are added.\n";
std::cout << "Enter a number for variable a\n";
std::cin >> a;
std::cout << "Now enter a number for variable b\n";
std::cin >> b;
if (a>10 and b<a) a - b;
else a+b;
std::cout << "The value of a is: " <<a;
std::cout << "Press any key to exit";
end = getchar();
return 0;
}
這是我用來編譯程序
cl /EHsc main.cpp
而最後的命令,但肯定不是最不重要的,錯誤的,這一名單程序提出,爲什麼這些錯誤在這裏我不知道。這對我來說沒有任何意義。
的main.cpp
error C2146: syntax error : missing ')' before identifier 'and'
error C2065: 'and' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'b'
error C2059: syntax error : ')'
error C2146: syntax error : missing ';' before identifier 'a'
warning C4552: '<' : operator has no effect; expected operator with side-effect
warning C4552: '-' : operator has no effect; expected operator with side-effect
error C2181: illegal else without matching if
warning C4552: '+' : operator has no effect; expected operator with side-effect
這些錯誤使每個人很奇怪。我以前從未遇到過,而且之前我從來沒有問過任何問題,因爲我一直能夠不問問題就能找到我的答案,但在這一方面我真的很難過。
'a + b'和'a-b'加減兩個變量,但不會對結果做任何事情。它們是沒有任何副作用的語句,例如將結果存儲在變量中或傳遞給函數調用,所以它們不會影響程序流。事實上,你的編譯器可能會將它們完全從程序中刪除,因爲它們的存在或缺失根本不會影響程序。 – Frxstrem
不要採取這種錯誤的方式,但你真的不需要這麼長的解釋來解釋你的問題。信息的信噪比並不是那麼好。 – StoryTeller
你意識到這一點,是&C在C + +?如果在Mac上編譯的代碼,他們真的有線編譯器那裏... – anhoppe