0

How to define LogLevel variable l?

Currently I have error:

 Error  3   error C2228: left of '.LogLevelDebug' must have class/struct/union  

Code:

typedef enum LogLevel
{
    LogLevelDebug = 0,
    LogLevelError = 1,
    LogLevelInfo = 2,
    LogLevelTrace = 3,
} LogLevel;



int main ()
{
logLevel l = LogLevel.LogLevelDebug;
}
3
  • 1
    I think "logLevel l" needs to be "LogLevel l"? Also, this question seems to be more about enumerations than unions. Commented Dec 2, 2014 at 17:31
  • 2
    It's an enum, not a union. Just LogLevelDebug. Commented Dec 2, 2014 at 17:31
  • Remove the typedef. It's not needed in C++, confuses people and makes things difficult, as you have found out. Commented Dec 2, 2014 at 18:09

1 Answer 1

3

In c++11 it would be:

enum class LogLevel
{
 LogLevelDebug = 0,
 LogLevelError = 1,
 LogLevelInfo = 2,
 LogLevelTrace = 3,
};



int main ()
{
 LogLevel l = LogLevel::LogLevelDebug;
}

in a previous C++ standard:

enum LogLevel
{
    LogLevelDebug = 0,
    LogLevelError = 1,
    LogLevelInfo = 2,
    LogLevelTrace = 3,
};

int main ()
{
    LogLevel l = LogLevelDebug;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh my goodness I've been in C# so long I'd forgotten about ::
Last I read, even in C++03 enums can be read as a legal scope on their names (ie, LogLevel::LogLevelDebug may be legal in C++03). Something about the enum being part of a more general class of thing... I guess it matters more if your compiler and potential future compilers support it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.