I'm a complete newbie to C++ so bear with me. I am defining a class called 'logging' which is currently write in main.cpp. Now that I am trying to split implementation (log.cpp) from declaration (log.h), I have the following situation that bring me to compiler error 'identifier logging is undefined':
log.cpp
#include <iostream>
#include <unordered_map>
#include <string>
#include "..\h\log.h"
class logging {
public:
std::unordered_map<int, std::string> m_log_levels;
logging() {
m_log_levels[2] = "info";
m_log_levels[1] = "warning";
m_log_levels[0] = "error";
}
private:
int m_level = 2;
public:
void setLevel(int level) {
m_level = level;
}
void log(const char* message) {
std::string sm = "[" + m_log_levels.at(m_level) + "]: ";
std::cout << sm + message << std::endl;
}
};
log.h
#pragma once
#include <unordered_map>
#include <string>
class logging {
public:
std::unordered_map<int, std::string> m_log_levels;
private:
int m_level;
public:
void setLevel(int level);
void log(const char* message);
};
main.cpp
#include <iostream>
#include "..\h\log.h"
int main() {
logging logger; /* -> identifier logging is undefined */
}
Build started...
1>------ Build started: Project: learn, Configuration: Debug x64 ------
1>log.cpp 1>main.cpp
1>C:\Users\pietr\source\repos\learn\learn\src\main.cpp(8,2): error C2065: 'logging': undeclared identifier
1>C:\repos\learn\learn\src\main.cpp(8,10): error C2146: syntax error: missing ';' before identifier 'logger'
1>C:\repos\learn\learn\src\main.cpp(8,10): error C2065: 'logger': undeclared identifier 1>Generating Code...
1>Done building project "learn.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========