 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
stuarte9
Joined: Mon Mar 08, 2010 5:03 pm Posts: 31 Location: Scotland
|
Posted: Fri Jan 13, 2012 2:50 pm Post subject: A problem with an enum |
|
|
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: |
enum boolean {false, true};
|
File "stack.h" is included in the (main) C++ file "test_stack.cpp" using the following :-
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,
Stuart |
|
| Back to top |
|
 |
larcky
Joined: Sun Nov 21, 2010 6:28 pm Posts: 19 Location: England
|
Posted: Fri Jan 13, 2012 7:53 pm Post subject: |
|
|
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
*EDIT*
Oops, sorry, see you've just tried that. Could you post a small test case? |
|
| Back to top |
|
 |
Xelous
Joined: Thu Apr 19, 2012 3:46 pm Posts: 16
|
Posted: Thu Apr 19, 2012 3:49 pm Post subject: |
|
|
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: |
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
// Enum definition with two new
// values for false and true
// respectively
enum boolean { false2, true2 };
#endif // STACK_H_INCLUDED
|
| Code: | #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: |
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: |
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#endif
|
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.
Regards
Xel |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|