Assigning [custom typdef] from an incompatible type 'int'

In the method of my main.c file, I declare the variable irq_raised, which is of type irq_type. I defined irq_type in typedef in another file and #import 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 them:

 irq_raised = IRQ_NONE; 

However, when I try to do the following:

 irq_raised |= IRQ_HBLANK; 

I get an error message:

 Assigning to 'irq_type' from incompatible type 'int' 

Why is this?

+4
source share
1 answer

In C ++, you cannot assign an int to a directly enumerated value without translation. The bitwise OR operation you perform results in an int, which then tries to assign to a variable of type irq_type without a cast. This is the same problem you have here:

 irq_type irq = 0; // error 

Instead, you can do the result:

 irq_type irq = IRQ_NONE; irq = (irq_type)(irq | IRQ_HBLANK); 

Relevant Specification Information:

The enumerator can be assigned an integer value. However, converting an integer to a counter requires an explicit cast, and the results are not defined.

+6
source

Source: https://habr.com/ru/post/1414753/


All Articles