简体   繁体   English

qt QFlags:有符号值超出枚举常量的范围

[英]qt QFlags: signed value is out of range for enum constant

I got following warnings for the enum value xxxxx920P4 building a qt C++ project.我在构建 qt C++ 项目时收到了枚举值xxxxx920P4以下警告。 And I found out in the debugging that xxxxx920P4 is now equals to xxxxxundefine ( 0x00000000 ), which causes the unexpected result.而且我在调试中发现xxxxx920P4现在等于xxxxxundefine0x00000000 ),这导致了意想不到的结果。 How should I resolve this problem?我应该如何解决这个问题?

I'm using Qt version: 4.8.7 and Visual studio 2010.我使用的是 Qt 版本:4.8.7 和 Visual Studio 2010。

The following loop will not be executed.下面的循环不会被执行。 gVersions[0].verNum is "xxxxx920P4" , so I guess "xxxxx920P4" 's value has been truncated and now it equals to 0x00000000 . gVersions[0].verNum"xxxxx920P4" ,所以我猜"xxxxx920P4"的值已被截断,现在它等于0x00000000

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

Doc for Qt Qflags class: https://doc.qt.io/qt-5/qflags.html Qt Qflags 类的文档: https ://doc.qt.io/qt-5/qflags.html

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)

The last value is too long to fit into aa non-long int.最后一个值太长long无法放入非长整数。 While newer compilers may try to choose a larger integral type for it, some older ones would complain.虽然较新的编译器可能会尝试为其选择更大的整数类型,但一些较旧的编译器会抱怨。 GCC tends to do the former unless you choose type explicitly.除非您明确选择类型,否则 GCC 倾向于使用前者。 Note that second to last value 0x80000000 or any combination of masks involving it would not fit into int , it's essentially a negative -2147483648.请注意,倒数第二个值 0x80000000 或涉及它的任何掩码组合都不适合int ,它本质上是负数 -2147483648。

 enum Version : long long  
 {
     //...
     xxxxx920P4   = 0x100000000,
 }; 

PS.附注。 Apparently Qt5 doesn't support something different from ( unsigned ) int : https://doc.qt.io/qt-5/qflags.html显然 Qt5 不支持与 ( unsigned ) int不同的东西: https ://doc.qt.io/qt-5/qflags.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM