How to declare an enum type?

Discussion in 'C++' started by bashill.zhu@gmail.com, Sep 26, 2008.

  1. Guest

    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 !
    , Sep 26, 2008
    #1
    1. Advertising

  2. 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
    Hendrik Schober, Sep 26, 2008
    #2
    1. Advertising

  3. Guest

    On 9ÔÂ26ÈÕ, ÏÂÎç4ʱ59·Ö, Hendrik Schober <> wrote:
    > 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
    , Sep 26, 2008
    #3
  4. Guest

    On 9ÔÂ26ÈÕ, ÏÂÎç7ʱ03·Ö, Pete Becker <> wrote:
    > On 2008-09-26 04:51:56 -0400, ""
    > <> 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.
    , Sep 26, 2008
    #4
  5. On 2008-09-26 15:49, wrote:
    > On 9月26æ—¥, 下åˆ7æ—¶03分, Pete Becker <> wrote:
    >> On 2008-09-26 04:51:56 -0400, ""
    >> <> 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
    Erik Wikström, Sep 26, 2008
    #5
  6. Guest

    On 9ÔÂ27ÈÕ, ÉÏÎç2ʱ19·Ö, Pete Becker <> wrote:
    > On 2008-09-26 09:41:53 -0400, ""
    > <> 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;
    }
    , Sep 27, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    337
  2. Eric Sosman
    Replies:
    3
    Views:
    770
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    482
  4. Lew
    Replies:
    0
    Views:
    524
  5. Roedy Green
    Replies:
    0
    Views:
    717
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page