Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A problem with an enum

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
stuarte9



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

PostPosted: Fri Jan 13, 2012 2:50 pm    Post subject: A problem with an enum Reply with quote

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 :-

Code:


#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,

Stuart
Back to top
View user's profile Send private message
larcky



Joined: Sun Nov 21, 2010 6:28 pm
Posts: 19
Location: England

PostPosted: Fri Jan 13, 2012 7:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
Xelous



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

PostPosted: Thu Apr 19, 2012 3:49 pm    Post subject: Reply with quote

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 user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
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
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast