views:

167

answers:

5

Hi, 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 ?

+7  A: 

Your best bet is something like this:

enum Operations
{
  Plus=4,
  Minus,
  Mod,
  LessThan,
  MoreThan
}
Blindy
+3  A: 

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   // >
};
Bojan Resnik
+2  A: 

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
}
Drakonite
+4  A: 

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.

jae
Seems I misread the +=4, but then... my C is obviously a bit rusty. ;-) Interesting, though, is the variety of styles visible in just these few examples (and... I'd certainly would spread it over more than a line in actual (production) code. But then again, Python doesn't have enums anyway ;-))
jae
I don't think you misread it. `+= 4` as *increment by four* is a completely reasonable interpretation.
wallyk
+1  A: 

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.

Felix