C++ Design Pattern

How to fix compile errors with VS2022 please?

Following programs are from Professional C++ 5th edition by Marc Gregoire.


Error C2653 'Logger': is not a class or namespace name
Logger.cpp 10

Thanks,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 // LoggerTest.cpp

import logger;

using namespace std;

class Foo
{
public:
	explicit Foo(ILogger& logger) : m_logger{ logger } { }

	void doSomething()
	{
		m_logger.log("Hello dependency injection!", ILogger::LogLevel::Info);
	}

private:
	ILogger& m_logger;
};

int main()
{
	Logger concreteLogger{ "log.out" };
	concreteLogger.setLogLevel(ILogger::LogLevel::Debug);

	Foo f{ concreteLogger };
	f.doSomething();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ILogger.cppm

export module ilogger;

import <string_view>;

// Definition of a logger interface.
export class ILogger
{
public:
	// Virtual destructor.
	virtual ~ILogger() = default;

	// Enumeration for the different log levels.
	enum class LogLevel {
		Error,
		Info,
		Debug
	};

	// Sets the log level.
	virtual void setLogLevel(LogLevel level) = 0;

	// Logs a single message at the given log level.
	virtual void log(std::string_view message, LogLevel logLevel) = 0;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Logger.cpp

export module logger;

import <stdexcept>;
import <format>;

using namespace std;

Logger::Logger(string_view logFilename)
{
	m_outputStream.open(logFilename.data(), ios_base::app);
	if (!m_outputStream.good()) {
		throw runtime_error{ "Unable to initialize the Logger!" };
	}
}

Logger::~Logger()
{
	m_outputStream << "Logger shutting down." << endl;
	m_outputStream.close();
}

void Logger::setLogLevel(LogLevel level)
{
	m_logLevel = level;
}

string_view Logger::getLogLevelString(LogLevel level) const
{
	switch (level) {
	case LogLevel::Error: return "ERROR";
	case LogLevel::Info: return "INFO";
	case LogLevel::Debug: return "DEBUG";
	}
	throw runtime_error{ "Invalid log level." };
}

void Logger::log(string_view message, LogLevel logLevel)
{
	if (m_logLevel < logLevel) {
		return;
	}

	m_outputStream << format("{}: {}", getLogLevelString(logLevel), message) << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Logger.cppm

export module logger;

export import ilogger;
import <string_view>;
import <fstream>;

export class Logger : public ILogger
{
public:
	explicit Logger(std::string_view logFilename);
	virtual ~Logger();
	void setLogLevel(LogLevel level) override;
	void log(std::string_view message, LogLevel logLevel) override;

private:
	// Converts a log level to a human readable string.
	std::string_view getLogLevelString(LogLevel level) const;

	std::ofstream m_outputStream;
	LogLevel m_logLevel{ LogLevel::Error };
};



Last edited on
First snippet (LoggerTest.cpp):
Line 3: You're trying to import logger;
Line 10: You reference type ILogger, but never imported it.
Last edited on
Thank you AbstractionAnon.
Fixed the import & exports correctly.
And the program is compiling and running.

Thanks again,
Topic archived. No new replies allowed.