Home >Backend Development >C++ >C++ syntax error: Enumeration members need to be initialized within parentheses, what should I do?

C++ syntax error: Enumeration members need to be initialized within parentheses, what should I do?

WBOY
WBOYOriginal
2023-08-22 15:41:04847browse

C++ syntax error: Enumeration members need to be initialized within parentheses, what should I do?

C is a common programming language whose syntax is relatively rigorous and easy to learn and apply. However, during specific programming, it is inevitable to encounter various errors. One of the common errors is "enumeration members need to be initialized within parentheses".

In C, the enumeration type is a very convenient data type, which can define a set of constants with discrete values, such as:

enum Color {RED, YELLOW, GREEN};

In this example, we define An enumeration type Color is created, which contains three enumeration constants: RED, YELLOW and GREEN. By default, the values ​​of enumeration constants are 0, 1, and 2.

However, when we want to assign a special value to an enumeration constant, we need to initialize it within brackets, such as:

enum ErrorCode {OK = 0, ERROR_FILE_NOT_FOUND = 2, ERROR_OUT_OF_MEMORY = 5};

In this example, we define An enumeration type ErrorCode is created, which contains three enumeration constants: OK, ERROR_FILE_NOT_FOUND and ERROR_OUT_OF_MEMORY. Among them, the value of OK is 0, the value of ERROR_FILE_NOT_FOUND is 2, and the value of ERROR_OUT_OF_MEMORY is 5.

However, sometimes we forget to initialize when defining enumeration constants, and the error "enumeration members need to be initialized within parentheses" occurs. For example, this error will occur in the following code:

enum Direction {UP, DOWN, LEFT, RIGHT, UNKNOWN = -1};

int main() {
    Direction d = LEFT;
    return 0;
}

In this example, we define an enumeration type Direction, which contains four enumeration constants: UP, DOWN, LEFT, and RIGHT. At the same time, we assign a special value -1 to UNKNOWN. However, because we forgot to specify specific values ​​for UP, DOWN, LEFT and RIGHT, the compiler will prompt an error "enum members need to be initialized within parentheses".

So, how should we deal with this error? A simple method is to specify a specific value for all enumeration constants, such as:

enum Direction {UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, UNKNOWN = -1};

In this example, we specify specific values ​​for all enumeration constants, so that the compiler does not An error will be prompted.

Another way is to use the enum class newly introduced in C 11. enum class is a type-safe enumeration type that forces all enumeration constants to specify a specific type, and the scope of the enumeration constants is limited to the enumeration type. For example:

enum class Direction : int {UP, DOWN, LEFT, RIGHT, UNKNOWN = -1};

int main() {
    Direction d = Direction::LEFT;
    return 0;
}

In this example, we define a Direction of enum class type, which specifies the type of enumeration constant as int. At the same time, we specify a special value -1 for UNKNOWN. When using enumeration constants, we need to add the prefix of the enumeration type, such as Direction::LEFT. In this way, even if we forget to specify a specific value for an enumeration constant, the compiler will not prompt an error.

In conclusion, the problem that enumeration members in C need to be initialized within parentheses can be solved by specifying a specific value for all enumeration constants or using the enum class type. We should avoid this kind of error when programming to ensure the correctness and robustness of the program.

The above is the detailed content of C++ syntax error: Enumeration members need to be initialized within parentheses, what should I do?. 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