I yesterday asked the following question: Error C2059: syntax error 'constant' [duplicate]
The code is:
enum {false,true};
typedef char bool;
I now know why I get the error but I have no solution to my problem. Any idea will be appreciate.
I yesterday asked the following question: Error C2059: syntax error 'constant' [duplicate]
The code is:
enum {false,true};
typedef char bool;
I now know why I get the error but I have no solution to my problem. Any idea will be appreciate.
Simply delete those two lines. Any code that uses bool
, true
or false
will still compile, since these are keywords in C++.
The only problem could be if some evil code relies on this bool
type being able to store other values. However, such code is almost certainly wrong anyway.
If you are using the same code for both C++ and C projects, then you have to conditionally remove those declarations depending on compiler. This can be done with the preprocessor like this:
#if !defined(__cplusplus) && !defined(__bool_true_false_are_defined)
enum {false,true};
typedef char bool;
#endif
When compiling with a C++ compiler, the preprocessor macro __cplusplus
will be defined, but it will never be defined in a C compiler. The preprocessor macro __bool_true_false_are_defined
is defined if you include <stdbool.h>
which also defines the boolean types and values.
In fact, I suggest you don't do your own declarations at all, but if you're not compiling with a C++ compiler, then simply include <stdbool.h>
instead.