Invalid conversion from int to enum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AmmarN
    New Member
    • Mar 2007
    • 18

    Invalid conversion from int to enum

    HI All;

    I am having a problem changing the value of an enum. I have a struct as shown below:

    Code:
      typedef struct
      {
      union 
         { long lV; volatile char c[8]; } u1;
      
      union 
      {
        enum  { CDR_RESET, CDR_ON, CDR_DISARMED } iS;
        volatile char c[8] } u2;
     } cdr_t;
    Now i have an array of this struct say:

    cdr_t array[4];

    array[0].u2.iS=1;

    When i try to change the value of the enum "iS" in the struct i get an error saying that """"" Invalid conversion from int to cdr_t ::<anonymous
    union>::<anonym ous enum>'""""""

    Please help. Its a c++ programme.Thank s in advance.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Try
    Code:
    array[0].u2.iS = static_cast<iS>(1);
    But actually, it looks like you have provided the definition for an enum type (enum {details...} iS;), but never declared a variable of type iS within the union.

    Comment

    • AmmarN
      New Member
      • Mar 2007
      • 18

      #3
      Originally posted by Ganon11
      Try
      Code:
      array[0].u2.iS = static_cast<iS>(1);
      But actually, it looks like you have provided the definition for an enum type (enum {details...} iS;), but never declared a variable of type iS within the union.

      Thanks alot for your reply,i tried it with the static_cast but i always get an error in the same line where i use the static_cast.
      for ex:
      Code:
      array[0].u2.iS = static_cast<iS>(1);
      Error:

      error: syntax error before `>' token

      Is there any other way to get this solved. I am working on a code that was written in C , i can access the value of this enum when i try to print it e.g

      cout<<array[0].u2.iS;
      say i get '1' as output. Now if i wouldl ike to change the value to 2 and save in this array how would i do that?? This is what i am trying to acheive.

      Thanks once again.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by Ganon11
        Try
        Code:
        array[0].u2.iS = static_cast<iS>(1);
        But actually, it looks like you have provided the definition for an enum type (enum {details...} iS;), but never declared a variable of type iS within the union.
        Ganon you have mistaken a type name for an unnamed enum, in fact the exact reverse of what you postulate has happened, i.e. declared a variable of type but never named the type.

        You do not have to name unions, structures (or I assume classes) if you create a variable of that type immediately. This is what the cdr_t structure contains, 2 unnamed unions and an unnamed enum.

        So array[0].u2.iS is a enum of unnamed type. Since the type isn't named it will be impossible to cast to this enum type. Additionally unnamed enums and structures result in less infomation when you use a symbolic debugger.

        I suggest you name your unions and enums (by putting the type name between union and { and enum and {) and then you will be able to cast to that type.

        Comment

        • AmmarN
          New Member
          • Mar 2007
          • 18

          #5
          Originally posted by Banfa
          Ganon you have mistaken a type name for an unnamed enum, in fact the exact reverse of what you postulate has happened, i.e. declared a variable of type but never named the type.

          You do not have to name unions, structures (or I assume classes) if you create a variable of that type immediately. This is what the cdr_t structure contains, 2 unnamed unions and an unnamed enum.

          So array[0].u2.iS is a enum of unnamed type. Since the type isn't named it will be impossible to cast to this enum type. Additionally unnamed enums and structures result in less infomation when you use a symbolic debugger.

          I suggest you name your unions and enums (by putting the type name between union and { and enum and {) and then you will be able to cast to that type.

          Hey ,

          It still doesnot work, I named the unions and the enum and then tried the same code to cast the value, but it still gives the same error.
          Thanks.

          Comment

          • nmadct
            Recognized Expert New Member
            • Jan 2007
            • 83

            #6
            This worked for me. It's just a slight rearrangement of your code, in which I named the enumeration so I can cast a value to it:

            Code:
            enum cdr_enum { CDR_RESET, CDR_ON, CDR_DISARMED };
            
            typedef struct
            {
                    union { long lV; volatile char c[8]; } u1;
            
                    union
                    {
                            cdr_enum iS;
                            volatile char c[8];
                    } u2;
            } cdr_t;
            
            int main()
            {
                    cdr_t myvar;
                    myvar.u2.iS = static_cast<cdr_enum>(1);
                    return 0;
            }
            (Just implementing what Banfa suggested.)

            Comment

            • AmmarN
              New Member
              • Mar 2007
              • 18

              #7
              Originally posted by nmadct
              This worked for me. It's just a slight rearrangement of your code, in which I named the enumeration so I can cast a value to it:

              Code:
              enum cdr_enum { CDR_RESET, CDR_ON, CDR_DISARMED };
              
              typedef struct
              {
                      union { long lV; volatile char c[8]; } u1;
              
                      union
                      {
                              cdr_enum iS;
                              volatile char c[8];
                      } u2;
              } cdr_t;
              
              int main()
              {
                      cdr_t myvar;
                      myvar.u2.iS = static_cast<cdr_enum>(1);
                      return 0;
              }
              (Just implementing what Banfa suggested.)

              Thank you very much all for your time and help. I got it figured out the exact same way you suggested. Its just that I didnot wanna declare the enum outside of the struct. I thought if i could find something that would do it without haveing to define the enum first outside of the struct. But it seems like thats the only way. Thanks once again for your time and help.

              You guys are great.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by AmmarN
                Its just that I didnot wanna declare the enum outside of the struct.
                You don't have to declare the union outside of the struct you could do it like this

                Code:
                struct cdr_t
                {
                        union { long lV; volatile char c[8]; } u1;
                
                        union u2_name
                        {
                                enum cdr_enum { 
                                        CDR_RESET, 
                                        CDR_ON, 
                                        CDR_DISARMED 
                                } iS;
                                volatile char c[8];
                        } u2;
                };
                
                int main()
                {
                        cdr_t myvar;
                        myvar.u2.iS = static_cast<cdr_t::u2_name::cdr_enum>(1);
                        return 0;
                }
                Notice I have named the union too.

                Comment

                • AmmarN
                  New Member
                  • Mar 2007
                  • 18

                  #9
                  Originally posted by Banfa
                  You don't have to declare the union outside of the struct you could do it like this

                  Code:
                  struct cdr_t
                  {
                          union { long lV; volatile char c[8]; } u1;
                  
                          union u2_name
                          {
                                  enum cdr_enum { 
                                          CDR_RESET, 
                                          CDR_ON, 
                                          CDR_DISARMED 
                                  } iS;
                                  volatile char c[8];
                          } u2;
                  };
                  
                  int main()
                  {
                          cdr_t myvar;
                          myvar.u2.iS = static_cast<cdr_t::u2_name::cdr_enum>(1);
                          return 0;
                  }
                  Notice I have named the union too.

                  THANK YOU VERY MUCH BANFA!!
                  I REALLY REALLY APPRECIATE YOUR HELP AND TIME. This site and you people are simply amazing.
                  Thanks again and yes it does work.

                  Comment

                  Working...