I created an enum like this:
enum class CATEGORIES{
C01 = 0x00000001,
C02 = 0x00000002,
C03 = 0x00000004,
...
C26 = 0x02000000,
C27 = 0x04000000,
C28 = 0x08000000,
C29 = 0x10000000,
C30 = 0x20000000,
C31 = 0x40000000,
C32 = 0x80000000, //Error at this line
}
Enumerator value evaluates to 2147483648, which cannot be narrowed to type 'int'
How should I resolve it? I just wanted a convenient way of representing the categories, and enum class seemed to make sense for namespacing..
CodePudding user response:
Someone suggested the following and then deleted their comment, but it seemed to work, making the underlying type unsigned int:
enum class CATEGORIES : uint32_t{
...
}
CodePudding user response:
0x80000000
is 2147483648
in base 10, and an int
cannot contain 2147483648
You should do this:
enum class CATEGORIES : unsigned long{
C01 = 0x00000001,
C02 = 0x00000002,
C03 = 0x00000004,
...
C26 = 0x02000000,
C27 = 0x04000000,
C28 = 0x08000000,
C29 = 0x10000000,
C30 = 0x20000000,
C31 = 0x40000000,
C32 = 0x80000000,
}; // also you seem to miss this semicolon
CodePudding user response:
Without specifying explicitly the underlying type of CATEGORIES
is int
, and 0x80000000
(2147483648
) is outside of the range of int
on your platform.
You can specify a larger type (e.g. long
; depending on your needs) as the underlying type explicitly.
enum class CATEGORIES : long {
C01 = 0x00000001,
C02 = 0x00000002,
C03 = 0x00000004,
...
C26 = 0x02000000,
C27 = 0x04000000,
C28 = 0x08000000,
C29 = 0x10000000,
C30 = 0x20000000,
C31 = 0x40000000,
C32 = 0x80000000,
};