在c ++中使用enum

时间:2014-12-02 17:29:45

标签: c++ visual-c++

如何定义LogLevel变量l?

目前我有错误:

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

代码:

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



int main ()
{
logLevel l = LogLevel.LogLevelDebug;
}

1 个答案:

答案 0 :(得分:3)

c++11中,它将是:

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



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

在之前的C ++标准中:

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

int main ()
{
    LogLevel l = LogLevelDebug;
}