Search code examples
enumscaplcanoe

Operand types are incompatible inside enum {...} - Compiler error in definition of variable?


Code: (running on CANoe 8.5.98 32bit)

/*@!Encoding:1252*/
includes {
}
variables {
    /* Windows HRESULT error type enum.                         */
    enum HRESULT_Types {
        S_OK            /* Operation successful                 */ = 0x00000000,
        S_FALSE         /* No error occured, oper. unsuccessful */ = 0x00000001,
        E_NOTIMPL       /* Not implemented                      */ = 0x80004001,
        E_NOINTERFACE   /* No such interface supported          */ = 0x80004002,
        E_POINTER       /* Pointer that is not valid            */ = 0x80004003,
        E_ABORT         /* Operation aborted                    */ = 0x80004004,
        E_FAIL          /* Unspecified failure                  */ = 0x80004005,
        E_UNEXPECTED    /* Unexpected failure                   */ = 0x8000FFFF,
        E_ACCESSDENIED  /* General access denied error          */ = 0x80070005,
        E_HANDLE        /* Handle that is not valid             */ = 0x80070006,
        E_OUTOFMEMORY   /* Failed to allocate necessary memory  */ = 0x8007000E,
        E_INVALIDARG    /* One or more arguments are not valid  */ = 0x80070057
    };
    msTimer Timer10ms;
}

on start {
    setTimer(Timer10ms, 10);
}

on timer Timer10ms {
    long hr = S_OK;
    hr = func1();
    if(hr == S_OK) {
        setTimer(Timer10ms, 10);
    }
}

long func1() {
    return S_OK;
}

Compiler error:

System L7,C3: operand types are incompatible.

What have I done so far:

  • I don't have any double declaration of S_OK.

  • I tried commenting the S_OK enum and the compiler said unknown symbol 'S_OK' everywhere I wrote S_OK in the code.

  • I can write SS_OK instead of S_OK and it compiles without errors.

  • I can write S_OK = 0 and the same error appears.

  • I can write S_OK, ... and the same error appears.

Good to know:

I have built a dll that implements some features from the Windows API to my CANoe environment. Of course there is the typedef of S_OK, but I have no access to any other typedefs or global variables, since the CAPL dlls are very restrictive. That's why I wanted to implement this simple enum.


Can someone explain me why the compiler doesn't want to compile this correctly? I have no Idea, this error seems so weird to me (incompatible types inside a definition ¯\_(ʘ ͟ʖʘ)_/¯).


Solution

  • The problem is that func1() returns a long, but S_OK is an enumeration constant. Hence the error "operand types are incompatible". Here are two options to fix the error:

    1. Cast S_OK to long:
    long func1() {
        return (long)S_OK;
    }
    
    1. (Preferred) Change the return type to enum HRESULT_Types:
    enum HRESULT_Types func1() {
        return S_OK;
    }
    

    You can also declare/define variables associated with an enumeration type:

    on timer Timer10ms {
        enum HRESULT_Types hr = S_OK; // instead of 'long hr = S_OK' if using option 2
        ....
    }