0

error messages like:undefined identifier for m_LogLevel variable expected identifiers from line 40-46.

I have tried using the log functions without the fullstops. I simply dont have any more ideas in my reviewing i did not see any issues within the log.

#include <iostream>

class Log {

   private:
        const int LogLevelWarning = 1;
        const int LogLevelError = 0;
        const int LogLevelInfo = 2;

    private:
        int Loglevel = LogLevelInfo;
    public:
        void SetLevel(int level) {
            int m_Loglevel;
        }
        void Info(const char* message) {
    
            if (m_LogLevel >= LogLevelInfo) {
                std::cout << "[Info]" << message << "\n";
            }
        }
        void Warn(const char* message) {
            if (m_LogLevel >= LogLevelWarning) {
                std::cout << "[WARNING]" << message << "\n";
            }
        }
    
        void Error(const char* message) {
            if (m_LogLevel >= Lo

gLevelError) {
            std::cout << "[Errror]" << message << "\n";
        }
    }
};

int main() {
    
        
        Log log;
        Log.SetLevel(Log.LogLevelWarning);
        Log.Warn("Hello!");
        Log.Info("Hey!")
        Log.Error("Hi!")
}
mch
  • 9,424
  • 2
  • 28
  • 42
  • 5
    There are multiple problems with the shown code. Unfortunately, "watching tutorials" will not help. C++ is the most complicated general purpose programming language in use today. Any clown can publish some scribblings on a web site, or upload a rambling video to Youtube. But the only effective way to learn C++ is by taking an organized, rigorous, guided study regimen [from a quality ***edited*** C++ textbook](https://stackoverflow.com/questions/388242/), that will explain underlying C++ fundamentals, in detail, and give sample practice programs, with answers. – Sam Varshavchik Jul 05 '21 at 12:38

2 Answers2

1
void SetLevel(int level) {
        int m_Loglevel;
}

The scope of m_Loglevel is just restricted to the SetLevel function. It won't be visible anywhere else. You can read more about scopes here C++ Scope
Also you're just declaring it with no initialization. It just contains garbage value.
You should rather set the 'LogLevel' to be 'level' which is passed as parameter. Like so:

 void SetLevel(int level) {
        LogLevel = level;
 }

And then ofcourse, replace 'm_LogLevel' with just 'LogLevel' in other functions aswell.

Staz
  • 348
  • 1
  • 2
  • 6
0

Rewrite code: Instead of

private:
        int Loglevel = LogLevelInfo;
    public:
        void SetLevel(int level) {
            int m_Loglevel;
        }

write as follow:

private:
        int m_Loglevel = LogLevelInfo;
    public:
        void SetLevel(int level) {
            m_Loglevel = level;
        }
Evgeny
  • 1,072
  • 6
  • 6