Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFLAGS : signed value is out of range for enum constant
Forum Updated to NodeBB v4.3 + New Features

QFLAGS : signed value is out of range for enum constant

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MNGL

    Hi, All

    I got following warnings for the enum value "xxxxx920P4". And I found out in the debugging that 'xxxxx920P4' is now equals to "xxxxxundefine"( 0x00000000), which causes the unexpected result. How should I resolve this problem? thank.

    warning C4341: 'xxxxx920P4' : signed value is out of range for enum constant

    warning C4309: 'initializing' : truncation of constant value

     enum Version
     {
         xxxxxundefine   = 0x00000000,
         xxxxx400     = 0x00000001,
         xxxxx401     = 0x00000002,
         xxxxx410     = 0x00000004,
         xxxxx411     = 0x00000008,
         xxxxx412     = 0x00000010,
         xxxxx420     = 0x00000020,
         xxxxx430     = 0x00000040,
         xxxxx431     = 0x00000080,
         xxxxx432     = 0x00000100,
         xxxxx440     = 0x00000200,
         xxxxx500     = 0x00000400,
         xxxxx510     = 0x00000800,
         xxxxx520     = 0x00001000,
         xxxxx521     = 0x00002000,
         xxxxx600     = 0x00004000,
         xxxxx611     = 0x00008000,
         xxxxx620     = 0x00010000,
         xxxxx621     = 0x00020000,
         xxxxx700     = 0x00040000,
         xxxxx910     = 0x00080000,
         xxxxx910P5   = 0x00100000,
         xxxxx910P6   = 0x00200000,
         xxxxx910P11  = 0x00400000,
         xxxxx910P12  = 0x00800000,
         xxxxx910P13  = 0x01000000,
         xxxxx910P14  = 0x02000000,
         xxxxx910P15  = 0x04000000,
         xxxxx910P16  = 0x08000000,
         xxxxx920          = 0x10000000,
         xxxxx920P1   = 0x20000000,
         xxxxx920P2   = 0x40000000,
         xxxxx920P3   = 0x80000000,
         xxxxx920P4   = 0x100000000,
     }; Q_DECLARE_FLAGS(Versions, Version)
    
    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #4

    @MNGL said in QFLAGS : signed value is out of range for enum constant:

    warning C4341: 'xxxxx920P4' : signed value is out of range for enum constant

    As @Christian-Ehrlicher already sayed, xxxxx920P4 = 0x100000000 needs at least 33 bit, this cannot be hold in an integer which have 32 bit ;)

    You can try to force enum type to long long: enum Version : qint64 { ... }, this should works with C++ 11 at least.

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    1 Reply Last reply
    1
    • M Offline
      M Offline
      MNGL
      wrote on last edited by
      #5

      Qt version: 4.8.7

      Compiler: Visual studio 2010:

      Following loop will not executed. gVersions[0].verNum is "xxxxx920P4", so I guess "xxxxx920P4"'s value has been truncated and now it equals to "0x00000000".
      for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
      {

      }
      
      jsulmJ 1 Reply Last reply
      0
      • M MNGL

        Qt version: 4.8.7

        Compiler: Visual studio 2010:

        Following loop will not executed. gVersions[0].verNum is "xxxxx920P4", so I guess "xxxxx920P4"'s value has been truncated and now it equals to "0x00000000".
        for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
        {

        }
        
        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @MNGL said in QFLAGS : signed value is out of range for enum constant:

        for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)

        Well, what do you think will happen if "gVersions[i].verNum != xxxxxundefine" never becomes false?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • jsulmJ jsulm

          @MNGL said in QFLAGS : signed value is out of range for enum constant:

          for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)

          Well, what do you think will happen if "gVersions[i].verNum != xxxxxundefine" never becomes false?

          M Offline
          M Offline
          MNGL
          wrote on last edited by
          #7

          @jsulm said in QFLAGS : signed value is out of range for enum constant:

          xxxxxundefine

          "xxxxxundefine" is also in the gVersions[], "gVersions[i].verNum != xxxxxundefine" will become false.

          jsulmJ 1 Reply Last reply
          0
          • M MNGL

            @jsulm said in QFLAGS : signed value is out of range for enum constant:

            xxxxxundefine

            "xxxxxundefine" is also in the gVersions[], "gVersions[i].verNum != xxxxxundefine" will become false.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @MNGL said in QFLAGS : signed value is out of range for enum constant:

            will become false

            Well, if you are sure.
            Else your app will crash with out of bounds exception. It is always a good idea to check the index for being in range.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MNGL
              wrote on last edited by
              #9

              Unfortunately, I'm the guy who gonna maintain this project, the guy who design this did not expect the enum value will be out of range in less than 40 items.

              JonBJ 1 Reply Last reply
              0
              • M MNGL

                Unfortunately, I'm the guy who gonna maintain this project, the guy who design this did not expect the enum value will be out of range in less than 40 items.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #10

                @MNGL said in QFLAGS : signed value is out of range for enum constant:

                did not expect the enum value will be out of range in less than 40 items

                Your enum is a bit-flags enum. It does not hold sequential values (1, 2, 3, 4, ...), it holds sequential bits (1, 2, 4, 8, ...). So if you only give it 32-bits it will hold... up to 32 values. 40 items is not going to fit into it!

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  MNGL
                  wrote on last edited by
                  #11

                  Hi, guys. Is there any example on using QBitArray to replace QFlag(bitmask enum in my case)?

                  jsulmJ 1 Reply Last reply
                  0
                  • M MNGL

                    Hi, guys. Is there any example on using QBitArray to replace QFlag(bitmask enum in my case)?

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @MNGL Did you try what @KroMignon suggested?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @MNGL Did you try what @KroMignon suggested?

                      M Offline
                      M Offline
                      MNGL
                      wrote on last edited by MNGL
                      #13

                      @jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.

                      Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                      jsulmJ KroMignonK 2 Replies Last reply
                      0
                      • M MNGL

                        @jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.

                        Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @MNGL Well, QBitArray and QFlag are two different things. You can't use QBitArray in code which expects QFlag.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @MNGL Well, QBitArray and QFlag are two different things. You can't use QBitArray in code which expects QFlag.

                          M Offline
                          M Offline
                          MNGL
                          wrote on last edited by
                          #15

                          @jsulm

                          In below post, BitArray is mentioned as alternative solution.
                          https://stackoverflow.com/questions/1060760/what-to-do-when-bit-mask-flags-enum-gets-too-large

                          I guess I have to get ride of following QFlag code and rewrite the whole thing.

                          if(Versions.testFlag(xxxxx920P4) )

                          1 Reply Last reply
                          0
                          • M MNGL

                            @jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.

                            Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                            KroMignonK Offline
                            KroMignonK Offline
                            KroMignon
                            wrote on last edited by
                            #16

                            @MNGL said in QFLAGS : signed value is out of range for enum constant:

                            Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                            Just out of curiosity, it is not easier to change the "nature" of the enum and use plain values and not bit field?
                            Do you really need a bit field?

                            It maybe easier to replace the bitfield enum with and plain value enum and use QList/QVector on situation where you need to handle multiple values.

                            Just my 2 cts.

                            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              MNGL
                              wrote on last edited by
                              #17
                                   typedef enum
                                  {
                                      Undefined  = 0x00000000,
                                      H1      = 0x00000001,
                                      H2      = 0x00000002,
                                      H3      = 0x00000004,
                               
                                  } Hardware;
                                  Q_DECLARE_FLAGS(Hardwares, Hardware)
                              
                                  typedef struct
                                  {
                                      Version          verNum ;
                                      Hardwares   supportedHW ;
                              	    
                                  } Settings ;
                              
                                  const Settings gSettings[] =
                                 {                                    
                                     {   xxxxx400 ,         H1|H2|H3  },
                                     {   xxxxx401 ,         H1|H3   },
                                 } ;
                              

                              Now the "Hardwares" type is to store combinations of Hardware values. Due to the range limit, I want to change "supportedHW" to a different data structure. I tired following type:

                               QList<Hardware>     supportedHW;   //Got compiling errors:error C2552: 'supportedHW' : non-aggregates cannot be initialized with initializer list
                               Hardware  supportedHW[4] ;   //pass compile, but not easy to use as QList and QFlags
                              

                              Any suggestions? Thanks.

                              jsulmJ 1 Reply Last reply
                              0
                              • M MNGL
                                     typedef enum
                                    {
                                        Undefined  = 0x00000000,
                                        H1      = 0x00000001,
                                        H2      = 0x00000002,
                                        H3      = 0x00000004,
                                 
                                    } Hardware;
                                    Q_DECLARE_FLAGS(Hardwares, Hardware)
                                
                                    typedef struct
                                    {
                                        Version          verNum ;
                                        Hardwares   supportedHW ;
                                	    
                                    } Settings ;
                                
                                    const Settings gSettings[] =
                                   {                                    
                                       {   xxxxx400 ,         H1|H2|H3  },
                                       {   xxxxx401 ,         H1|H3   },
                                   } ;
                                

                                Now the "Hardwares" type is to store combinations of Hardware values. Due to the range limit, I want to change "supportedHW" to a different data structure. I tired following type:

                                 QList<Hardware>     supportedHW;   //Got compiling errors:error C2552: 'supportedHW' : non-aggregates cannot be initialized with initializer list
                                 Hardware  supportedHW[4] ;   //pass compile, but not easy to use as QList and QFlags
                                

                                Any suggestions? Thanks.

                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #18

                                @MNGL @KroMignon asked you whether your enum really has to contain bitfields: means 2^x values (0001, 0010, 0100, ...).
                                If this is not required then use just normal numbers, then your range is what an int provides and is for sure enough.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved