Home > front end >  enum in struct by c
enum in struct by c

Time:08-24

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"

CodePudding user response:

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.

CodePudding user response:

You cannot assign values ​​to enums, they are just used as numbers

Example:

switch(i) // i -> int

case OPERATOR_TYPE::ADD :// 0. Index

break; case OPERATOR_TYPE::SUB:// 1. Index

break;

  • Related