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`?

1
  • 3
    You've found one of the reasons why C++ cannot be made 100% backwards compatible with C.
    – Gorpik
    Sep 4, 2013 at 12:52

2 Answers 2

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() {}
1
  • 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). Sep 13, 2016 at 12:18
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.