-7

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.

Community
  • 1
  • 1
RayOldProf
  • 1,040
  • 4
  • 16
  • 40
  • 2
    If you're coding in C++ you already have `bool`, `true` and `false`. What are you trying to achieve ? – Nbr44 Sep 05 '13 at 08:52
  • Im trying to compile the code in c++ compiler, and then build a dll – RayOldProf Sep 05 '13 at 08:53
  • It's invalid C++ code, so you can't compile it with a C++ compiler. Why are you trying to compile these 2 lines with a C++ compiler ? – nos Sep 05 '13 at 08:54
  • @RezaAyadipanah: Well, you can't: it's not possible to use keywords as names. If you want to use a boolean type, just delete these and use the builtin `bool`, `true` and `false` keywords. If you want your own not-quite-boolean type for some weird reason, use different names. – Mike Seymour Sep 05 '13 at 08:55
  • Then you'll have to modify those two line, or use the preprocessor to remove them from your build (`#ifdef __cplusplus...`) – Nbr44 Sep 05 '13 at 08:55
  • You are asking for a solution, but you have not described the problem clearly. – David Heffernan Sep 05 '13 at 08:57
  • possible duplicate of [Error C2059: syntax error 'constant'](http://stackoverflow.com/questions/18614221/error-c2059-syntax-error-constant) – Qantas 94 Heavy Feb 15 '14 at 03:59

2 Answers2

2

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I get the following Error: error LNK2019: unresolved external symbol "char const * __cdecl nameOnly(char const *)" – RayOldProf Sep 05 '13 at 09:01
  • @RezaAyadipanah: That's an unrelated problem. Most likely, either the definition of that function is missing, or the file that contains it is not being linked into your build. [This question](http://stackoverflow.com/questions/12573816) might help. – Mike Seymour Sep 05 '13 at 09:05
1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621