Home > Backend Development > C++ > body text

C++ syntax error: Enumeration members must be unique, how should I modify it?

王林
Release: 2023-08-21 23:58:50
Original
1509 people have browsed it

When we write a C program, we sometimes encounter the following error message:

"C syntax error: enumeration members must be unique"

This is because in the enumeration Within a type, the names of enumeration members must be unique and cannot be repeated.

So, how to correct this error? Several solutions are given below.

Method 1: Manually modify the names of enumeration members

The simplest method is to manually modify the names of enumeration members to ensure that they are unique. For example, change duplicate member names to different names.

For example, if we define an enumeration type that contains two members with the same member name, as shown below:

enum Fruit
{
    Banana,
    Apple,
    Strawberry,
    Apple
};
Copy after login

The compiler will prompt "C syntax Error: Enumeration members must be unique". We can change one of the member names to a different name, as shown below:

enum Fruit
{
    Banana,
    Apple,
    Strawberry,
    Orange
};
Copy after login

This will resolve this error.

Method 2: Use typedef to define a new enumeration type

If we don’t want to modify the original enumeration type, we can also use typedef to define a new enumeration type and replace the original Enum types renamed.

For example, we can rename the above Fruit enumeration type to FruitType:

typedef enum
{
    Banana,
    Apple,
    Strawberry,
    Apple
} FruitType;
Copy after login

In this way, FruitType can be used in the program to replace the original Fruit enumeration type.

Method 3: Use enumeration class

C 11 introduces the new feature of enum class (enum class), which can limit the namespace of enumeration members when defining the enumeration type, so that This can avoid naming conflict problems.

For example, we can use enum class to define an enumeration type named Fruit and limit the namespace of its members to Fruit:

enum class Fruit
{
    Banana,
    Apple,
    Strawberry,
    Apple
};
Copy after login

At this time, if we define two enumeration types with the same name member, the compiler will report an error instead of a warning.

Summary:

In C, enumeration members must be unique and cannot be repeated. If the compiler prompts "C syntax error: enumeration members must be unique", you can manually modify the enumeration member names, use typedef to define a new enumeration type, or use an enumeration class to solve the problem. The method of using enumeration classes is a new feature introduced in C 11, which can effectively avoid the problem of naming conflicts of enumeration members.

The above is the detailed content of C++ syntax error: Enumeration members must be unique, how should I modify it?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template