A problem with an enum

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

A problem with an enum

Postby stuarte9 » Fri Jan 13, 2012 2:50 pm

Hi all,

I'm currently having a small compilation problem with an enum.

In the C header file "stack.h" I have the following :-

Code: Select all

enum boolean {false, true};

File "stack.h" is included in the (main) C++ file "test_stack.cpp" using the following :-

Code: Select all

#include "stack.h"

When I compile "test_stack.cpp" using "g++ -Wall -g test_stack.cpp -o test_stack" I get the following error messages :-

In file included from test_stack.cpp:5:0:
stack.h:8:10: error: expected identifier before ‘false’
stack.h:8:10: error: expected ‘}’ before ‘false’
stack.h:8:10: error: expected unqualified-id before ‘false’
stack.h:8:21: error: expected declaration before ‘}’ token

To be clear, #include "stack.h" occurs on line 5 of "test_stack.cpp".

Does anyone know if a C++ program including a C header is likely to lead to just this kind of error message ? I would have thought that, as C++ is a superset of C, this would not be the case.

Also, I just don't see anything wrong with the enum. I've tried changing boolean to "truth" and then "valid". (I wondered if the fact that bool is a keyword in C++ was the problem.) A re-compilation in each case merely resulted in the displaying of the same error messages.

Thanks in advance,

Posts: 54
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland

Postby larcky » Fri Jan 13, 2012 7:53 pm

Is this C or C++? C++ already has a type bool which is a keyword, as are true and false, so you're not supposed to use any of them as identifiers. What about calling them true2 and false2, for example?
Regards, l

Oops, sorry, see you've just tried that. Could you post a small test case?
Posts: 19
Joined: Sun Nov 21, 2010 6:28 pm
Location: England

Postby Xelous » Thu Apr 19, 2012 2:49 pm

Hey, new member to the boards, so only just saw your post...

I've given your problem a try, with the following basic files:

Code: Select all

// Enum definition with two new
// values for false and true
// respectively
enum boolean { false2, true2 };


Code: Select all
#include <stdio.h>
#include <stdlib.h>

#include "Stack.h"

int main()
    printf("Hello world!\n");
    return 0;

And I've used the build command:

Code: Select all
g++ -Wall main.c -o out.o

With absolutely no issues. As you have yourself pointed out you can not use "true" and "false" as label names for the enumeration values you define, you have the correct include.

I however wonder, have you missed off the use of

Code: Select all


And so are including the stack header more than once, and therefore, declaring the enum more than once - which you can't do?

Let me know if you need any help.


Posts: 16
Joined: Thu Apr 19, 2012 2:46 pm

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest