在 c 中使用枚举作为 +, - 等运算符时出现问题

发布于 2024-08-16 02:17:03 字数 168 浏览 1 评论 0原文

我正在尝试枚举一些运算符,我的代码行是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆头 2024-08-23 02:17:03

你最好的选择是这样的:

enum Operations
{
  Plus=4,
  Minus,
  Mod,
  LessThan,
  MoreThan
}

Your best bet is something like this:

enum Operations
{
  Plus=4,
  Minus,
  Mod,
  LessThan,
  MoreThan
}
情释 2024-08-23 02:17:03

枚举必须是标识符,不能使用裸符号。所以,

enum operations { inc_by_4, minus, modulus, less_than, greater_than };

会起作用的。 (我在猜测你想表达什么,我可能离得很远,但这就是猜测的本质。)

如果你能告诉我们你真正想做什么,我们可能可以更好地回答你。

Enums have to be identifiers, you can't use bare symbols. So,

enum operations { inc_by_4, minus, modulus, less_than, greater_than };

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.

顾挽 2024-08-23 02:17:03

不,你不能。您需要为它们分配名称,就像为任何标识符分配名称一样:

enum operations
{
    PLUS = 4, // +
    MINUS,    // -
    MOD,      // %
    LESS,     // <
    GREATER   // >
};

No, you can't. You need to assign them names, just as you would to any identifier:

enum operations
{
    PLUS = 4, // +
    MINUS,    // -
    MOD,      // %
    LESS,     // <
    GREATER   // >
};
习ぎ惯性依靠 2024-08-23 02:17:03

枚举是具有定义值的标识符列表。不能使用 +、=、<、> 等字符作为标识符名称。

您需要拼写出名称,例如:

enum Operators
{
  Plus,
  Equals,
  LessThan,
  GreaterThan
}

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 Operators
{
  Plus,
  Equals,
  LessThan,
  GreaterThan
}
冷清清 2024-08-23 02:17:03

此外,请考虑到在您的代码中

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文