Problem with enum and struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paolo

    #1

    Problem with enum and struct

    Hi all!
    As I said in the title, I have a very strange problem compiling a
    program Here's the code that gives the problem:

    enum letters
    {
    a = 0,
    b,
    c
    };

    typedef struct
    {
    letters let;
    int number;
    } someStruct;

    It gives me an error before letters in the struct. Any idea on
    what generates this error? I have to say that this was a C++ code, but
    it doesn't use any class or anything else incompatible with C, at least
    I think so.
    Thanks for the help!

  • Vladimir S. Oka

    #2
    Re: Problem with enum and struct


    Paolo wrote:[color=blue]
    > Hi all!
    > As I said in the title, I have a very strange problem compiling a
    > program Here's the code that gives the problem:
    >
    > enum letters
    > {
    > a = 0,
    > b,
    > c
    > };
    >
    > typedef struct
    > {
    > letters let;
    > int number;
    > } someStruct;
    >
    > It gives me an error before letters in the struct. Any idea on
    > what generates this error? I have to say that this was a C++ code, but
    > it doesn't use any class or anything else incompatible with C, at least
    > I think so.[/color]

    I don't know about C++ (ask in comp.lang.c++), but in C an `enum` does
    not define a type, so you'd need:

    enum letters let;

    in your structure.

    --
    BR, Vladimir

    Comment

    • Carl R. Davies

      #3
      Re: Problem with enum and struct

      Paolo wrote:[color=blue]
      > enum letters
      > {
      > a = 0,
      > b,
      > c
      > };
      >
      > typedef struct
      > {
      > letters let;
      > int number;
      > } someStruct;[/color]

      typedef struct
      {
      enum letters let;
      int number;
      } someStruct;

      or you could declare another typedef:

      typedef enum letters letters_e;

      then:

      typedef struct
      {
      letters_e let;
      int number;
      } someStruct;

      Comment

      • Paolo

        #4
        Re: Problem with enum and struct

        Thank you, I think I resolved. Another question, if I have a method
        that returns an enum value, for example

        letters GetFirstLetter( char* words);

        do I have to add enum before letters?

        Comment

        • Carl R. Davies

          #5
          Re: Problem with enum and struct

          Paolo wrote:[color=blue]
          > Thank you, I think I resolved. Another question, if I have a method
          > that returns an enum value, for example
          >
          > letters GetFirstLetter( char* words);
          >
          > do I have to add enum before letters?[/color]

          IMHO yes. The word 'letters' alone doesn't denote a type, it must be
          'enum letters' for it to be recognised as such. I always prefer
          typedef(ing) enums and appending '_e', like 'letters_e'.

          Comment

          • Fred Kleinschmidt

            #6
            Re: Problem with enum and struct


            "Paolo" <paomic@gmail.c om> wrote in message
            news:1141310386 .740513.216130@ u72g2000cwu.goo glegroups.com.. .[color=blue]
            > Hi all!
            > As I said in the title, I have a very strange problem compiling a
            > program Here's the code that gives the problem:
            >
            > enum letters
            > {
            > a = 0,
            > b,
            > c
            > };
            >[/color]

            If you want to use "letters" as a type name, as you do in someStruct below,
            you have to typedef it.
            typedef enum {
            a=0,
            b,
            c
            } letters;
            [color=blue]
            > typedef struct
            > {
            > letters let;
            > int number;
            > } someStruct;
            >
            > It gives me an error before letters in the struct. Any idea on
            > what generates this error? I have to say that this was a C++ code, but
            > it doesn't use any class or anything else incompatible with C, at least
            > I think so.
            > Thanks for the help!
            >[/color]
            --
            Fred L. Kleinschmidt
            Boeing Associate Technical Fellow
            Technical Architect, Software Reuse Project


            Comment

            • Micah Cowan

              #7
              Re: Problem with enum and struct

              "Paolo" <paomic@gmail.c om> writes:
              [color=blue]
              > Thank you, I think I resolved. Another question, if I have a method
              > that returns an enum value, for example
              >
              > letters GetFirstLetter( char* words);
              >
              > do I have to add enum before letters?[/color]

              Yes, in C.

              In your orinal post, you claimed that the code was C++ (which is
              off-topic here, since /this/ group discusses only C). If that were
              true, your code should have compiled (as would the above). It seems
              very likely that you were compiling your "C++" code as C code, which
              is why our solution worked. If it's really C++ code, make sure you're
              compiling it with a C++ compiler. For implementations playing multiple
              roles, this probably means you need to make sure your file ends in
              ..cc, .cx, or .C, rather than .c.

              A lot of what I've just said is not really related to what we discuss
              here on this newsgroup. If you would like your code to be C++ code,
              and want to ask further questions about C++ code, ask
              comp.lang.c++. If you want to ask how to get your compiler to compile
              the code as C++ code, ask on a group related to your compiler.

              HTH
              Micah

              Comment

              • Keith Thompson

                #8
                Re: Problem with enum and struct

                "Vladimir S. Oka" <novine@btopenw orld.com> writes:[color=blue]
                > Paolo wrote:[color=green]
                >> Hi all!
                >> As I said in the title, I have a very strange problem compiling a
                >> program Here's the code that gives the problem:
                >>
                >> enum letters
                >> {
                >> a = 0,
                >> b,
                >> c
                >> };[/color][/color]
                [...][color=blue]
                >
                > I don't know about C++ (ask in comp.lang.c++), but in C an `enum` does
                > not define a type, so you'd need:
                >
                > enum letters let;
                >
                > in your structure.[/color]

                Yes, an enum does define a type. The name of the type is
                "enum letters", not "letters".

                <OT>In C++, the type can be referred to as just "letters"; likewise
                for classes, unions, and structs. This is one of the differences
                between C and C++.</OT>

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                Working...