gpt4 book ai didi

c - 重新声明枚举器

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:26 24 4
gpt4 key购买 nike

海湾合作委员会 4.1.2 c99

我在这个文件 ccsmd.h 中有以下枚举:

enum options_e
{
acm = 0,
anm,
smd,
LAST_ENTRY,

ENTRY_COUNT = LAST_ENTRY
};

enum function_mode_e
{
play = 0,
record,
bridge,
LAST_ENTRY,

ENTRY_COUNT = LAST_ENTRY
};

错误信息:

error: redeclaration of enumerator ‘LAST_ENTRY’
error: previous definition of ‘LAST_ENTRY’ was here
error: redeclaration of enumerator ‘ENTRY_COUNT’
error: previous definition of ‘ENTRY_COUNT’ was here

我有 LAST_ENTRY,因此我可以将其用作数组的索引。所以我喜欢在所有枚举中保持相同。

最佳答案

枚举值存在于与定义枚举相同的命名空间中。也就是说,关于 LAST_ENTRY,它类似于(非常在这里松散地使用):

enum options_e { /* ... */ );

// for the LAST_ENTRY value in options_e
static const int LAST_ENTRY = /* whatever */;

enum function_mode_e { /* ... */ );

// for the LAST_ENTRY value in function_mode_e
static const int LAST_ENTRY = /* whatever */;

如您所见,您正在重新定义 LAST_ENTRY,因此会出现错误。最好在您的枚举值前加上一些区分它们的前缀:

enum options_e
{
options_e_acm = 0,
options_e_anm,
options_e_smd,
options_e_LAST_ENTRY,
options_e_ENTRY_COUNT = options_e_LAST_ENTRY // note this is redundant
};

enum function_mode_e
{
function_mode_e_play = 0,
function_mode_e_record,
function_mode_e_bridge,
function_mode_e_LAST_ENTRY,

function_mode_e_ENTRY_COUNT = function_mode_e_LAST_ENTRY
};

虽然现在你失去了你以前想要的一切。 (你能澄清一下那是什么吗?)

关于c - 重新声明枚举器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com