How to declare an enum type?

B

bashill.zhu

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 !
 
H

Hendrik Schober

[...]
$ 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
 
B

bashill.zhu

[...]
$ 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

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
 
B

bashill.zhu

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.
 
E

Erik Wikström

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.
 
B

bashill.zhu

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top