I learned c++ on a mac, and have recently transferred over to windows 7. I downloaded the windows v7.1 sdk and ran the installer. Its the .net 4 dependent version of the sdk, and I have .net 4 installed.
I am using the command line because I prefer using it, I did that with the gcc compiler on the mac and I got pretty good at it considering I am pretty new to programing.
I have been using the v7.1 sdk developer command prompt because it sets up the environment variables using the SetEnv batch file.
The compiler is obviously the cl.exe compiler from Microsoft.
I ran the typical and very simple hello world program, including a getchar() at the end to let me actually see the program, something new since mac didn't require that. And the getchar worked fine, the program compiled and ran fine.
The problem showed up when I tried compiling some source code I wrote on the mac. Which compiled fine on the mac by the way. It started throwing up some really weird errors, such as telling me that the logical 'and' operator is an undefined identifier. Now I could be the stupid one here, but from my understanding the and operator is NOT an identifier, it's an operator.
So I decided to narrow down the problem by writing a very simple program that makes use of one if statement and one else statement and the 'and' operator and see what happens. Below is the code I tried to compile:
//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;
}
And this is the command I used to compile the program
cl /EHsc main.cpp
And last but certainly not least, the list of errors that this program raised, why these errors are here I am not sure. It just doesn't make any sense to me.
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
Every last one of these errors is weird. I have never encountered it before, and I have never asked a question before because I have always been able to find my answer without asking, but on this one I am really stumped.
This is a bug (a feature) in Microsoft Visual C++ compiler - it does not support keywords
and
,and_eq
,bitand
,bitor
,compl
,not
,not_eq
,or
,or_eq
,xor
,xor_eq
. You should use more commonly used operators like&&
instead ofand
,||
instead ofor
etc. Equivalence table:Unlike C++, C does not provide those keywords, instead it provides a header
<iso646.h>
with a set of macros with those names that expand to those logical operators. It was done to provide support for machines that did not have required characters on a keyboard back in the past.Because C++ tries to avoid macros as much as possible, C++ header equivalent
<ciso646>
does not define any macros, instead those are provided as builtin keywords.As was noted here, newer versions of MSVC might add some support for this, but you should know that those "altenative operators" are rarely used. I advise you to stick with original C syntax.