Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to declare an enum type?

Reply
Thread Tools

How to declare an enum type?

 
 
bashill.zhu@gmail.com
Guest
Posts: n/a
 
      09-26-2008
bzhu@TY-PC /h/working/tcplex/ch8/testenum
$ cat lexer.h
namespace Lexer
{
enum Token_value;
extern Token_value string_value;
void get_token();
}

bzhu@TY-PC /h/working/tcplex/ch8/testenum
$ cat lexer.cpp
#include "lexer.h"
namespace Lexer{
enum Token_value{
one,two
};
}
void Lexer::get_token()
{
}



bzhu@TY-PC /h/working/tcplex/ch8/testenum
$ g++ -c lexer.cpp
In file included from lexer.cpp:1:
lexer.h:3: error: use of enum `Token_value' without previous
declaration
lexer.h:4: error: `Token_value' does not name a type

Thanks !

 
Reply With Quote
 
 
 
 
Hendrik Schober
Guest
Posts: n/a
 
      09-26-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> [...]
> $ g++ -c lexer.cpp
> In file included from lexer.cpp:1:
> lexer.h:3: error: use of enum `Token_value' without previous
> declaration


That should probably read "definition".

> lexer.h:4: error: `Token_value' does not name a type
>
> Thanks !


What's your point?

Schobi
 
Reply With Quote
 
 
 
 
bashill.zhu@gmail.com
Guest
Posts: n/a
 
      09-26-2008
On 9月26日, 下午4时59分, Hendrik Schober <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > [...]
> > $ g++ -c lexer.cpp
> > In file included from lexer.cpp:1:
> > lexer.h:3: error: use of enum `Token_value' without previous
> > declaration

>
> That should probably read "definition".
>
> > lexer.h:4: error: `Token_value' does not name a type

>
> > Thanks !

>
> What's your point?
>
> Schobi


I want write a modual that expose a "string_value" var which type is
enum Token_value.
and a function "get_token" which
returns a Token_value var.
I modify the code,but it still has errors:
Administrator@HILL /m/working/tcplex/ch8/testenum
$ cat lexer.h
namespace Lexer
{
enum Token_value{
one,two
};
extern Token_value string_value;
Token_value get_token();
}

Administrator@HILL /m/working/tcplex/ch8/testenum
$ cat lexer.cpp
#include "lexer.h"
namespace Lexer{
Token_value string_value;
}
Token_value Lexer::get_token()
{
return one;
}



Administrator@HILL /m/working/tcplex/ch8/testenum
$ g++ -c lexer.cpp
lexer.cpp:5: error: `Token_value' does not name a type
 
Reply With Quote
 
bashill.zhu@gmail.com
Guest
Posts: n/a
 
      09-26-2008
On 9月26日, 下午7时03分, Pete Becker <(E-Mail Removed)> wrote:
> On 2008-09-26 04:51:56 -0400, "(E-Mail Removed)"
> <(E-Mail Removed)> said:
>
>
>
>
>
> > bzhu@TY-PC /h/working/tcplex/ch8/testenum
> > $ cat lexer.h
> > namespace Lexer
> > {
> > enum Token_value;
> > extern Token_value string_value;
> > void get_token();
> > }

>
> > bzhu@TY-PC /h/working/tcplex/ch8/testenum
> > $ cat lexer.cpp
> > #include "lexer.h"
> > namespace Lexer{
> > enum Token_value{
> > one,two
> > };
> > }
> > void Lexer::get_token()
> > {
> > }

>
> > bzhu@TY-PC /h/working/tcplex/ch8/testenum
> > $ g++ -c lexer.cpp
> > In file included from lexer.cpp:1:
> > lexer.h:3: error: use of enum `Token_value' without previous
> > declaration
> > lexer.h:4: error: `Token_value' does not name a type

>
> You'd have the same problem if you used a forward declaration of a
> class in place of an enum. In general, you can't create objects of
> incomplete types, and "enum Token_value;" declares an incomplete type.
> The code should have the complete definition of Token_value before it's
> used.
>
> --
> Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
> Standard C++ Library Extensions: a Tutorial and Reference
> (www.petebecker.com/tr1book)- 隐藏被引用文字 -
>
> - 显示引用的文字 -


I am wonder this reason , I just want declare a varible ,it's not
definition.
 
Reply With Quote
 
Erik Wikstr枚m
Guest
Posts: n/a
 
      09-26-2008
On 2008-09-26 15:49, (E-Mail Removed) wrote:
> On 9鏈26鏃, 涓嬪崍7鏃03鍒, Pete Becker <(E-Mail Removed)> wrote:
>> On 2008-09-26 04:51:56 -0400, "(E-Mail Removed)"
>> <(E-Mail Removed)> said:
>>
>>
>>
>>
>>
>> > bzhu@TY-PC /h/working/tcplex/ch8/testenum
>> > $ cat lexer.h
>> > namespace Lexer
>> > {
>> > enum Token_value;
>> > extern Token_value string_value;
>> > void get_token();
>> > }

>>
>> > bzhu@TY-PC /h/working/tcplex/ch8/testenum
>> > $ cat lexer.cpp
>> > #include "lexer.h"
>> > namespace Lexer{
>> > enum Token_value{
>> > one,two
>> > };
>> > }
>> > void Lexer::get_token()
>> > {
>> > }

>>
>> > bzhu@TY-PC /h/working/tcplex/ch8/testenum
>> > $ g++ -c lexer.cpp
>> > In file included from lexer.cpp:1:
>> > lexer.h:3: error: use of enum `Token_value' without previous
>> > declaration
>> > lexer.h:4: error: `Token_value' does not name a type

>>
>> You'd have the same problem if you used a forward declaration of a
>> class in place of an enum. In general, you can't create objects of
>> incomplete types, and "enum Token_value;" declares an incomplete type.
>> The code should have the complete definition of Token_value before it's
>> used.


Please do not quote signatures.

>
> I am wonder this reason , I just want declare a varible ,it's not
> definition.


Because the compiler does not know the size of a variable of type
Token_value until it has seen its definition. This means that you can
only declare pointers to Token_value (since the size of a pointer ti
known), but not variables.

--
Erik Wikstr枚m
 
Reply With Quote
 
bashill.zhu@gmail.com
Guest
Posts: n/a
 
      09-27-2008
On 9月27日, 上午2时19分, Pete Becker <(E-Mail Removed)> wrote:
> On 2008-09-26 09:41:53 -0400, "(E-Mail Removed)"
> <(E-Mail Removed)> said:
>
>
>
>
>
>
>
> > Administrator@HILL /m/working/tcplex/ch8/testenum
> > $ cat lexer.cpp
> > #include "lexer.h"
> > namespace Lexer{
> > Token_value string_value;
> > }
> > Token_value Lexer::get_token()
> > {
> > return one;
> > }

>
> > Administrator@HILL /m/working/tcplex/ch8/testenum
> > $ g++ -c lexer.cpp
> > lexer.cpp:5: error: `Token_value' does not name a type

>
> The second mention of Token_value, as the compiler says, does not name
> a type. Nor does it name anything else. Its proper name is
> Lexer::Token_value.
>
> --
> Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
> Standard C++ Library Extensions: a Tutorial and Reference
> (www.petebecker.com/tr1book)- 隐藏被引用文字 -
>
> - 显示引用的文字 -


Thanks Pete and Eric:
I have got what I want .

$ cat lexer.h
namespace Lexer
{
enum Token_value{
one,two
};
extern Token_value string_value;
Token_value get_token();
}

bzhu@TY-PC /h/working/tcplex/ch8/testenum
$ cat lexer.cpp
#include "lexer.h"
namespace Lexer
{
Token_value string_value;
}
Lexer::Token_value Lexer::get_token()
{
return one;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to declare an enum type? bashill.zhu@gmail.com C++ 0 09-26-2008 08:52 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
How to declare enum using attribute in VC++ .Net? Woon Kiat C++ 5 08-18-2005 09:51 AM
enum within an enum - Java 6 06-13-2005 12:51 AM



Advertisments