I try to make enum for assembly instructions,
typedef enum opcode {load, loada, store, mov, add, sub, mul, div, mod, cmp, b, blt, ble, bne, beq, bge, bgt, read, write}OP_CODE;
but I get this error because, I think, some of the instruction is already define in C.
DEF.h:13:62: error: ‘div’ redeclared as different kind of symbol
typedef enum opcode {load, loada, store, mov, add, sub, mul, div, mod, cmp, b,
Does anyone know how to solve this problem. I cannot change the words or the case letters. Thanks
Since you are in C, cannot rename the words and cannot change the case, and you don't want also to mess with the libraries... no much choice.
You could create a struct
typedef struct {int load, loada, store, mov, add, sub, mul, div, mod, cmp, b,
blt, ble, bne, beq, bge, bgt, read, write;
} OP_CODE;
Assign member values
OP_CODE op;
op.load = 1;
op.loada = 2;
...
and use op.opcode wherever necessary.
The other workaround which will do exactly what you want is not very clean
#define have to be declared after the headers includesfor instance
#include <stdlib.h>
...
#define load 1
#define loada 2
....
Then you can use the names as is
if (opcode == div) {
// do div stuff
}
Not recommended but if you have to use the names as they are in C, that's the solution (note that a few defines are used a lot in C, like read and write to name two, to read and write files).
div is a library function, so you should not use the same name. So you should "change the words or the case letters" (or change the compiler to C++ and use namespace to isolate your enumeration).
You can rename all members of your enum, e.g.
typedef enum opcode {LOAD, LOADA, STORE, MOV, ADD, SUB, MUL, DIV, MOD, CMP, B, BLT, BLE, BNE, BEQ, BGE, BGT, READ, WRITE} OP_CODE;
or
typedef enum opcode {op_load, op_loada, op_store, op_mov, op_add, op_sub, op_mul, op_div, op_mod, op_cmp, op_b, op_blt, op_ble, op_bne, op_beq, op_bge, op_bgt, op_read, op_write} OP_CODE;
I prefer the last option (with prefix added to names of all values of opcode)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With