用字符串初始化枚举 [英] Initializing enum with strings

查看:86
本文介绍了用字符串初始化枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



gcc-3.4抱怨非整数:


enum {IDENTIFIER ="< identifier>",WIDGETDEF = widgetdef};


即使我将字符串转换为整数。

Hi,
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};

even if i cast the strings to integers.

推荐答案

Russell Shaw < rjshawN_o@s_pam.netspace.net.au>写道:
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
gcc-3.4抱怨非整数:

枚举{IDENTIFIER ="< identifier>",WIDGETDEF =" widgetdef" ;};


所以应该这样。枚举常量只能有整数值。

如果你想要枚举常量字符串值,你可能会尝试

做一些你不需要的东西,或者不应该这样做。为什么首先需要

这样的常量?

即使我把字符串转换为整数。
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
And so it should. Enumeration constants can only have integral values.
If you want enum constants with string values, you''re probably trying to
do something which you needn''t, or shouldn''t, do. Why would you need
such constants in the first place?
even if i cast the strings to integers.




这甚至没有意义。你会把什么转换成整数,一个指针

到一个字符串文字?从理论上讲,这是可能的 - 但它并没有让人感觉到。价值将毫无用处。


Richard



That doesn''t even make sense. What would you cast to integer, a pointer
to a string literal? That''s possible, in theory - but it doesn''t make
sense. The value will be useless.

Richard


Russell Shaw写道:
Russell Shaw wrote:

gcc-3.4抱怨非整数:

enum {IDENTIFIER ="< identifier>",WIDGETDEF =" widgetdef"};
<即使我把字符串转换成整数。
Hi,
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};

even if i cast the strings to integers.




那么,你期待什么?


枚举标识符有整数值。将一个字符串

转换为整数并不会让你感到非常有趣

或可靠。


-

Chris" electric hedgehog" Dollin

它被称为*极端*编程,而非*愚蠢*编程。



Well, what did you expect?

The enum identifiers have integer values. Casting a string
to an integer doesn''t get you anything terribly interesting
or reliable.

--
Chris "electric hedgehog" Dollin
It''s called *extreme* programming, not *stupid* programming.


Richard Bos写道:
Richard Bos wrote:
Russell Shaw< rjshawN_o@s_pam.netspace.net.au>写道:
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
gcc-3.4抱怨非整数:

枚举{IDENTIFIER ="< identifier>",WIDGETDEF =" widgetdef" ;};
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};



它应该如此。枚举常量只能有整数值。
如果你想要枚举常量字符串值,你可能会尝试做一些你不需要或不应该做的事情。为什么你首先需要这样的常量?



And so it should. Enumeration constants can only have integral values.
If you want enum constants with string values, you''re probably trying to
do something which you needn''t, or shouldn''t, do. Why would you need
such constants in the first place?




因为我在词法分析器中使用常量,他们可以做双重任务

用于错误消息打印输出。我需要在

代码中使用它们多个位置,如果我直接使用字符串,我不能保证每次都得到

相同的指针值。



Because i''m using the constants in a lexer, and they can do double duty
for error message printouts. I need to use them multiple places in the
code, and if i use the strings directly, i''m not guaranteed to get the
same pointer value every time.

即使我将字符串转换成整数。
even if i cast the strings to integers.



这甚至没有意义。你会把什么转换为整数,一个字符串文字的指针?从理论上说这是可能的 - 但它并没有意义。价值将毫无用处。

理查德



That doesn''t even make sense. What would you cast to integer, a pointer
to a string literal? That''s possible, in theory - but it doesn''t make
sense. The value will be useless.

Richard




char * str =" abcd" ;;声明一个指向字符串的指针,所以我想

使用像这样的指针作为枚举的内容。



char *str = "abcd"; declares a pointer to a string, so i wanted to
use pointers like this as inits for the enum.


这篇关于用字符串初始化枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆