在 c 中使用枚举作为 +, - 等运算符时出现问题
我正在尝试枚举一些运算符,我的代码行是:
enum operations{+=4,-,%,<,>}
当我尝试编译这一行时,gcc 说:“+”标记之前的预期标识符
那么,我如何枚举这些运算符。我们可以为它们使用一些转义字符吗?
I'm trying to enumerate some operators, my code line is :
enum operations{+=4,-,%,<,>}
when i'm trying to compile this line , gcc says : expected identifier before ‘+’ token
So, how can I enumerate these operators. Can we use some escape characters for them ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你最好的选择是这样的:
Your best bet is something like this:
枚举必须是标识符,不能使用裸符号。所以,
会起作用的。 (我在猜测你想表达什么,我可能离得很远,但这就是猜测的本质。)
如果你能告诉我们你真正想做什么,我们可能可以更好地回答你。
Enums have to be identifiers, you can't use bare symbols. So,
would work. (I'm guessing what you want to express, I'm probably way off, but that's the nature of guesswork.)
If you could tell us what you actually want to do, we probably can answer you better.
不,你不能。您需要为它们分配名称,就像为任何标识符分配名称一样:
No, you can't. You need to assign them names, just as you would to any identifier:
枚举是具有定义值的标识符列表。不能使用 +、=、<、> 等字符作为标识符名称。
您需要拼写出名称,例如:
An enumeration is a list of identifiers which have a defined value. You cannot use characters such as +, =, <, >, etc as names of identifiers.
You'll need to spell out the names, such as:
此外,请考虑到在您的代码中
enum Operations{+=4,-,%,<,>}
序列
+=
被解析为+= 赋值表达式运算符。这可以通过在+
和=
之间插入一个空格来帮助实现 - 只是这会产生另一个编译器错误。In addition, please take into account that in your code
enum operations{+=4,-,%,<,>}
the sequence
+=
is parsed as the += assignment expression operator. This could be helped by inserting a space between+
and=
- only this would yield yet another compiler error.