In a method in my main.c file, I declare the variable irq_raised, which is of the type irq_type. I've defined irq_type in a typedef in another file and #import it at the top of main.c.
typedef enum
{
IRQ_NONE = 0x0000,
IRQ_VBLANK = 0x0001,
IRQ_HBLANK = 0x0002,
IRQ_VCOUNT = 0x0004,
IRQ_TIMER0 = 0x0008,
IRQ_TIMER1 = 0x0010,
IRQ_TIMER2 = 0x0020,
IRQ_TIMER3 = 0x0040,
IRQ_SERIAL = 0x0080,
IRQ_DMA0 = 0x0100,
IRQ_DMA1 = 0x0200,
IRQ_DMA2 = 0x0400,
IRQ_DMA3 = 0x0800,
IRQ_KEYPAD = 0x1000,
IRQ_GAMEPAK = 0x2000,
} irq_type;
I can assign this variable to one of these like so:
irq_raised = IRQ_NONE;
However, when I attempt to do the following:
irq_raised |= IRQ_HBLANK;
I get the error:
Assigning to 'irq_type' from incompatible type 'int'
Why is this?
In C++ you cannot assign an int directly to an enumerated value without a cast. The bitwise OR operation you are performing results in an int, which you then attempt to assign to a variable of type irq_type
without a cast. It is the same problem as you would have here:
irq_type irq = 0; // error
You can cast the result instead:
irq_type irq = IRQ_NONE;
irq = (irq_type)(irq | IRQ_HBLANK);
Relevant info from the specification:
An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined.
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