[英]enum in struct by c++
I have segment of code which from C by compiling in C++. But it fail.我有一段代码,从 C 编译到 C++。但它失败了。 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" “[错误] JAMEXP.C@221,31:从‘int’到‘OPERATOR_TYPE’的无效转换 [-fpermissive]\r\n”
"[Error] JAMEXP.C@221,33: invalid conversion from 'int' to 'JAME_EXPRESSION_TYPE' [-fpermissive]\r\n" “[错误] JAMEXP.C@221,33:从‘int’到‘JAME_EXPRESSION_TYPE’的无效转换[-fpermissive]\r\n”
C++ does not allow implicit conversion from int
to enum
. C++ 不允许从int
到enum
的隐式转换。 You can use static_cast
.您可以使用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.请注意,在 C++ 中不需要 typedef 并且有范围 enums ,它们更具限制性,因此被认为使用起来更安全。
Implicit conversion from int to enum is not supported in C++. C++ 不支持从 int 到 enum 的隐式转换。 Utilize static cast.利用 static 铸件。 However, it would be preferable to state it clearly so that the cast is unnecessary:但是,它会比 state 更可取,这样就不需要强制转换:
YYSTYPE jam_null_expression = {ADD,JAM_ILLEGAL_EXPR_TYPE,0,0,0};
Keep in mind that typedefs are not required in C++ and that scoped enums, which are even more restrictive and are hence seen to be safer to employ, are available instead.请记住,C++ 中不需要 typedef,并且可以使用范围枚举,这些枚举更具限制性,因此使用起来更安全。
You cannot assign values to enums, they are just used as numbers您不能为枚举分配值,它们仅用作数字
Example:例子:
switch(i) // i -> int switch(i) // i -> int
case OPERATOR_TYPE::ADD:// 0. Index case OPERATOR_TYPE::ADD:// 0. 索引
break;休息; case OPERATOR_TYPE::SUB:// 1. Index case OPERATOR_TYPE::SUB:// 1. 索引
break;休息;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.