4

I have the following code in a header file:

enum {false,true};

and I have my main function in main.c. if I change the extention to main.cpp I get the following error:

Error C2059: syntax error 'constant' 

Im using visual c++, any Idea why`?

RayOldProf
  • 990
  • 2
  • 13
  • 40

2 Answers2

11

true and false are keywords representing constant values in C++. You cannot use them to name things such as enum values.

As an example, the following would compile

enum { false_, true_ };

int main() {}
juanchopanza
  • 216,937
  • 30
  • 383
  • 461
  • It is also true for other constants (enumeration for instance) that are already defined elsewhere (I encountered a problem when I have defined an enumeration value named NO_ERROR which is already defined defined in winerror.h). – Guy Avraham Sep 13 '16 at 12:18
1

false and true are reserve words in C++. You can't redefine it as variable.

haccks
  • 100,941
  • 24
  • 163
  • 252