Error: Expected primary expression before ‘enum’ C++

Enumeration is a user-defined data type used to declare an integer constant with a user-assigned name that enhances the program’s readability. Below is the enum syntax. Syntax of enum declaration There are mostly two types …

Error Expected primary expression before 'enum' C++

Enumeration is a user-defined data type used to declare an integer constant with a user-assigned name that enhances the program’s readability. Below is the enum syntax.

Syntax of enum declaration

enum enum_name { constant1, constant2, constant3, …… }

There are mostly two types of error experienced in enum that are discussed below: 

Parenthesis error

Primary expression error mostly occurs when the syntax follows wrong. e.g.

enum Direction(Up, Right, Down, Left);

And the write syntax is here.

enum class Direction
{   Up,
    Right,
    Down,
    Left
};

And call the class “Direction” by using conditions

1. Use semicolon inside enum

Error expected when semicolon “; “ used inside the enum. e.g., Error removes by removing semicolons inside braces.

    enum ArrayElements
    {
        MAX_ARRAY_SIZE = 5;
    };

    int anArray[MAX_ARRAY_SIZE];
    return 0;
}

Conclusion

In this article, we find the reason for the error “Expected primary expression before ‘enum’ ” and how to resolve this error, for example.

Categories C++

Leave a Comment