Enum class not accepting check

Asked

Viewed 45 times

3

Well I’m having a doubt I’m starting now at C++

I’m using an Enum class:

enum class TYPE_ENTER {
    ENTER_OK = 0x1,
    ENTER_WARNING = 0x2
};

but when I compile the function there is error C2678 in the function.

int EnterSession::CheckEnter(char *type, int id) {

    if (*(int*)type == TYPE_ENTER::ENTER_OK) {// Ocorre Erro em ==
        //Mycode
    }
    return 0;
}

At compile time error occurs in ==, it is possible to maintain Enum class or I will have to use Enum normal ?

1 answer

0


You must convert your Enum class to INT as well, or else convert the type variable to your Enum class, and you are casting wrong.

To convert string or char to INT, you must use the atoi function();

The code would look like this:

    int nType = atoi(type);

    std::cout << nType;
    if (nType == (int) TYPE_ENTER::ENTER_OK) {
        //código
    }
  • just to understand, if it were me to convert the type instead of Enum how would it look ? I tried using TYPE_ENTER TestType = (int)type; most occurs C2440

  • Friend, as you are casting to int, the Testtype should be INT and not TYPE_ENTER. You cannot compare an int type with an ENUM CLASS without casting.

  • 1

    If char *type do not point to an object of the type int, the expression *(int *)type results in undefined behavior.

  • The response was corrected with the appropriate tests done.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.