Home  >  Article  >  Backend Development  >  C++ syntax error: identifiers in enumerations must be integer constants, how to solve it?

C++ syntax error: identifiers in enumerations must be integer constants, how to solve it?

WBOY
WBOYOriginal
2023-08-22 10:27:171116browse

When programming in C, sometimes you will encounter the syntax error message "Identifiers in enumerations must be integer constants". This article explains the causes of this problem and possible solutions.

First of all, we need to clarify what an enumeration is. In C, an enumeration is a special data type used to define a collection of constants with discrete values. Each constant in the enumeration is assigned an integer value, with the first constant defaulting to 0 and the remaining constants incrementing in sequence. For example:

enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

In the above code, Weekday is the name of this enumeration type, Monday, Tuesday, etc. are its member constants, and they are assigned integer values ​​​​from 0 to 6 respectively.

However, when we define the enumeration by mistake and define the value of a member constant as a non-integer constant, such as a string or a floating point number, the message "Identifiers in the enumeration must be integers" will appear. "Constant" syntax error message. For example:

enum Fruit {Apple = 1, Banana = 2, Orange = "orange"}; //错误!

In the above code, Orange is defined as a string constant, so an error will be reported during compilation.

So, how to solve this problem? A simple workaround is to explicitly specify integer values ​​for all member constants instead of using the default incrementing method. For example:

enum Fruit {Apple = 1, Banana = 2, Orange = 3};

In the above code, we manually specify an integer value for each member constant to avoid errors with non-integer constants.

Another solution is to use an enum class instead of a normal enumeration. Enumeration classes are more strict than ordinary enumerations and do not allow implicit conversion of integer values, thus avoiding the above errors. For example:

enum class Fruit {Apple = 1, Banana = 2, Orange}; //Orange自动被分配整数值3

In the above code, we used enum class to define an enumeration of Fruit type, and no integer value was specified for Orange, but it was still assigned the integer value 3 because this is an enum Class behavior definition.

In short, when encountering the syntax error "identifiers in enumerations must be integer constants" in C programming, we can solve this by explicitly specifying the integer value of the member constant or using an enumeration class. question.

The above is the detailed content of C++ syntax error: identifiers in enumerations must be integer constants, how to solve it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn