0

I have segment of code which from C by compiling in C++. But it fail. Does anyone know to modify?

/* types of expressions */
typedef enum
{
    JAM_ILLEGAL_EXPR_TYPE = 0,
    JAM_INTEGER_EXPR,
    JAM_BOOLEAN_EXPR,
    JAM_INT_OR_BOOL_EXPR,
    JAM_ARRAY_REFERENCE,
    JAM_EXPR_MAX

} JAME_EXPRESSION_TYPE;

enum OPERATOR_TYPE
{
    ADD = 0,
    SUB,
    UMINUS,
    MULT,
    DIV,
    MOD,
    NOT,
    AND,
    OR,
    BITWISE_NOT,
    BITWISE_AND,
    BITWISE_OR,
    BITWISE_XOR,
    LEFT_SHIFT,
    RIGHT_SHIFT,
    DOT_DOT,
    EQUALITY,
    INEQUALITY,
    GREATER_THAN,
    LESS_THAN,
    GREATER_OR_EQUAL,
    LESS_OR_EQUAL,
    ABS,
    INT,
    LOG2,
    SQRT,
    CIEL,
    FLOOR,
    ARRAY,
    POUND,
    DOLLAR,
    ARRAY_RANGE,
    ARRAY_ALL
};

typedef enum OPERATOR_TYPE OPERATOR_TYPE;

typedef struct EXP_STACK
{
  OPERATOR_TYPE     child_otype;
  JAME_EXPRESSION_TYPE type;
  long              val;
  long              loper;      /* left and right operands for DIV */
  long              roper;      /* we save it for CEIL/FLOOR's use */
} EXPN_STACK;

#define YYSTYPE EXPN_STACK      /* must be a #define for yacc */

YYSTYPE jam_null_expression = {0,0,0,0,0};//line 221

I got message:
"[Error] JAMEXP.C@221,31: invalid conversion from 'int' to 'OPERATOR_TYPE' [-fpermissive]\r\n"
"[Error] JAMEXP.C@221,33: invalid conversion from 'int' to 'JAME_EXPRESSION_TYPE' [-fpermissive]\r\n"

4
  • 1
    C++ is less tolerant to conversions between enum and int. Either use an explicit cast, or add -fpermissive as suggested by your compiler. Commented Aug 23, 2022 at 10:52
  • ... and no need for the typedef Commented Aug 23, 2022 at 10:57
  • @Klaus it is useful in case of C code. Commented Aug 23, 2022 at 11:34
  • Should be: YYSTYPE jam_null_expression = { ADD, JAM_ILLEGAL_EXPR_TYPE, 0, 0, 0 }; Commented Aug 23, 2022 at 11:35

1 Answer 1

1

C++ does not allow implicit conversion from int to enum. You can use static_cast. However, anyhow it would be better to spell it out explicitly, then there is no need for the cast:

YYSTYPE jam_null_expression = {ADD ,JAM_ILLEGAL_EXPR_TYPE ,0,0,0};

Note that in C++ there is no need for the typedefs and that there are scoped enums, which are even more restrictive and for that reason considered more safe to use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.