C ++枚举类quint64类型

时间:2016-04-02 12:45:57

标签: c++ c++11 enums

我关注enum class

#ifndef UEPROTOCOLCOMMANDS_H
#define UEPROTOCOLCOMMANDS_H

enum class UeProcotolCommand
{
    INVALID_COMMAND=0x0000000000000000,

    SERVER_GET_CLIENTS_VERSIONS=0x0000000000000001,
    SERVER_CONNECTION_RESET=0x0000000000000002,

    CLIENT_GET_DATABASE_CREDENTIALS=0x1000000000000001,
    CLIENT_DISCOVER_COMM_SERVER=0x1000000000000002
};   // UeProtocolCommands

#endif // UEPROTOCOLCOMMANDS_H

并生成以下错误:

In file included from ../../../../uePOSCommProtocol/net/comm_protocol/uemessageheader.h:6:0,
                 from ../../../../uePOSCommProtocol/net/comm_protocol/uemessagerequest.h:6,
                 from ../../../../uePOSCommProtocol/ueposcommprotocol.h:8,
                 from ../../../uemainwindow.h:16,
                 from ../../../main.cpp:3:
../../../../uePOSCommProtocol/net/comm_protocol/ueprotocolcommands.h:11:37: error: enumerator value 1152921504606846977l is too large for underlying type 'int'
     CLIENT_GET_DATABASE_CREDENTIALS=0x1000000000000001,
                                     ^
../../../../uePOSCommProtocol/net/comm_protocol/ueprotocolcommands.h:12:33: error: enumerator value 1152921504606846978l is too large for underlying type 'int'
     CLIENT_DISCOVER_COMM_SERVER=0x1000000000000002
                                 ^
make: *** [main.o] Error 1
14:23:40: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project uePOSCommClient (kit: Desktop Qt 5.6.0 GCC 64bit)
When executing step "Make"

我需要告诉编译器枚举元素的类型为quint64,而不是默认的int。怎么样?

1 个答案:

答案 0 :(得分:2)

使用enum class : quin64 { ... }(请参阅enumeration declaration)。所以完整的定义应该是:

enum class UeProcotolCommand : quint64
{
    INVALID_COMMAND=0x0000000000000000,

    SERVER_GET_CLIENTS_VERSIONS=0x0000000000000001,
    SERVER_CONNECTION_RESET=0x0000000000000002,

    CLIENT_GET_DATABASE_CREDENTIALS=0x1000000000000001,
    CLIENT_DISCOVER_COMM_SERVER=0x1000000000000002
};   // UeProtocolCommands