Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to code a simple program containing of two enums in c++:
C++
#include <iostream>

using namespace std;

int main()
{
    enum userchoice
    {
        Toyota = 1,
        Lamborghini,
        Ferrari,
        Holden,
        Range Rover
    };
    
    enum quizlevels
    {
        Hardquestions = 1,
        Mediumquestions, 
        Easyquestions
    };  

    return 0;
}

but it says expected primary-expression before 'enum'.

What I have tried:

I have tried to search some solutions but i cant find any
Posted
Updated 7-Apr-21 23:23pm
v2

1) You don't define enums inside a method: they are declared outside
2) You cannot include a space in any name - function, variable, type, class, struct, or enum.
3) It's a good idea to use consistent naming conventions - and capitalizing the first letter of each word makes things a lot more readable!

C++
#include <iostream>

using namespace std;


enum userchoice
    {
    Toyota = 1,
    Lamborghini,
    Ferrari,
    Holden,
    RangeRover
    };

enum quizlevels
    {
    HardQuestions = 1,
    MediumQuestions,
    EasyQuestions
    };

int main()
    {
    return 0;
    }
 
Share this answer
 
Comments
iwanttoaskquestions 8-Apr-21 3:50am    
thank you very much.
OriginalGriff 8-Apr-21 3:57am    
You're welcome!
Richard MacCutchan 8-Apr-21 4:11am    
Nothing wrong with having enums inside functions in C/C++. I've been doing it for years.
OriginalGriff 8-Apr-21 4:38am    
You're right - it must have been the space in Range Rover that confused GDB when I tried his code.
Add the space back and the error returned is decidedly unhelpful ...
main.cpp: In function ‘int main()’:
main.cpp:8:1: error: expected primary-expression before ‘enum’
 enum userchoice
 ^~~~
Richard MacCutchan 8-Apr-21 4:42am    
This is what I got from Microsoft's compiler:
C:\Users\rjmac\Documents\VSCode\C++>cl -nologo -EHsc -RTC1 -W3  cp.cpp
cp.cpp
cp.cpp(9): error C2146: syntax error: missing '}' before identifier 'Rover'
cp.cpp(10): error C2143: syntax error: missing ';' before '}'
cp.cpp(19): error C2059: syntax error: 'return'
cp.cpp(20): error C2059: syntax error: '}'
cp.cpp(20): error C2143: syntax error: missing ';' before '}'
As correctly noted by Richard, the actual problem of your code is the blank in the identifier: 'Range Rover' is not a valid identifier. On the other hand, enums are allowed inside functions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900