我在 Mac 上学习了 c++,最近转移到了 Windows 7。我下载了 windows v7.1 sdk 并运行了安装程序。它是 .net 4 依赖版本的 sdk,我安装了 .net 4。
我使用命令行是因为我更喜欢使用它,我在 mac 上使用 gcc 编译器做到了这一点,考虑到我对编程很陌生,所以我很擅长它。
我一直在使用 v7.1 sdk 开发人员命令提示符,因为它使用 SetEnv 批处理文件设置环境变量。
编译器显然是微软的 cl.exe 编译器。
我运行了典型且非常简单的 hello world 程序,最后包括一个 getchar() 让我真正看到该程序,这是由于 mac 不需要的新东西。getchar 工作正常,程序编译并运行良好。
当我尝试编译我在 mac 上编写的一些源代码时,问题出现了。顺便说一句,它在mac上编译得很好。它开始抛出一些非常奇怪的错误,例如告诉我逻辑“与”运算符是未定义的标识符。现在我在这里可能是个愚蠢的人,但据我了解,and 运算符不是标识符,而是运算符。
所以我决定通过编写一个非常简单的程序来缩小问题的范围,该程序使用一个 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
最后但同样重要的是,这个程序引发的错误列表,为什么这些错误在这里我不确定。这对我来说没有任何意义。
主文件
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
这些错误中的每一个都很奇怪。我以前从未遇到过,也从未问过问题,因为我总是能够不问就找到答案,但在这个问题上,我真的很难过。