-3

My main method testing the class says "expression must have a class type" and when trying to pass on enum into a function call I get "type Not allowed".

#include <iostream>
#include <string>

using std::string;
using std::cout;

class Logger
{
public:
    enum LogLevel { ALL, INFO, WARNING, ERROR, NEEDED };
private:
    LogLevel Errorlevel = ALL;
    string LogLevelNames[5] = { "ALL","INFO","WARNING", "ERROR", "NEEDED" };
public:
    Logger(LogLevel level)
    {
        Errorlevel = level;
        if (Errorlevel <= INFO)
        {
            cout << "[" << LogLevelNames[Errorlevel] << "]: " << "LOGGER set to: " << LogLevelNames[Errorlevel] << std::endl;
        }
    }
    ~Logger()
    {
        if (Errorlevel <= WARNING)
        {
            cout << "[" << LogLevelNames[Errorlevel] << "]:" << " LOGGER destroyed" << std::endl;
        }
    }
    void log(LogLevel level, string message)
    {
            if (Errorlevel <= level)
            {
                cout << "[" << LogLevelNames[level] << "]: " << message << std::endl;
            }
    }
};

int main()
{
    Logger logger(Logger::LogLevel WARNING); // no error

    logger.log(Logger::LogLevel ERROR, "test ERROR"); //errors
    logger.log(Logger::LogLevel INFO, "test INFO"); //errors

    system("PAUSE");
}

I'm fairly sure this is being caused by my own inexperience, but I'm not sure how to improve this code.

  • 2
    What do you think `Logger::LogLevel ERROR` does? – Mooing Duck Jan 26 '18 at 21:48
  • 4
    The first only works because C++ parses that as a function declaration. You really would want to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because it seems like you are writing C++ using trial and error. – Rakete1111 Jan 26 '18 at 21:49
  • @Mooing Duck state that ERROR is a type specified by the enum LogLevel in the logger class. – Mayor Jellies Jan 26 '18 at 21:59

1 Answers1

4

You are missing some scope in your initialization of Logger and it's causing the compiler to not resolve your call to log(). Fix it like this.

int main()
{
    Logger logger(Logger::WARNING);

    logger.log(Logger::ERROR, "test ERROR");
    logger.log(Logger::INFO, "test INFO");

    return 0;
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23