Cant Get Header And Translation Unit Correct

I'm just learning i build a simple logging function in the Main.cpp file but i want to break it out into a Log.h and Log.cpp file but I'm struggling please help.

Initially I had the enum definition in the class once i moved it out it fixed alot but im still getting one error in the cpp

Severity Code Description Project File Line Suppression State
Error C2011 'Log': 'class' type redefinition Logging c:\users\ma_je\onedrive\documents\visual studio 2017\projects\logging\logging\log.cpp 5


Log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once

enum LogLevel : unsigned char
		{
			LogLevelError = 0,
			LogLevelWarning = 1,
			LogLevelInfo = 2
		};

	class Log
	{

	private:
		LogLevel m_LogLevel;

	public:
		void SetLevel(LogLevel Level);
		void Error(const char* Message);
		void Warning(const char* Message);
		void Info(const char* Message);
	};


Log.cpp
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
#include <iostream>
#include "Log.h"

class Log
{
private:
	LogLevel m_LogLevel;
public:

	void Log::SetLevel(LogLevel Level)
	{
		m_LogLevel = Level;
	}

	void Log::Error(const char* Message)
	{
		if (m_LogLevel >= LogLevelError)
			std::cout << "[ERROR]: " << Message << std::endl;
	}

	void Log::Warning(const char* Message)
	{
		if (m_LogLevel >= LogLevelWarning)
			std::cout << "[WARNING]: " << Message << std::endl;
	}

	void Log::Info(const char* Message)
	{
		if (m_LogLevel >= LogLevelInfo)
			std::cout << "[INFO]: " << Message << std::endl;
	}

};
Last edited on
Hello @madaxe2018

the problem is you defined the class twice.


do this in .cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Log::SetLevel(LogLevel Level)
	{
		m_LogLevel = Level;
	}

	void Log::Error(const char* Message)
	{
		if (m_LogLevel >= LogLevelError)
			std::cout << "[ERROR]: " << Message << std::endl;
	}

	void Log::Warning(const char* Message)
	{
		if (m_LogLevel >= LogLevelWarning)
			std::cout << "[WARNING]: " << Message << std::endl;
	}

	void Log::Info(const char* Message)
	{
		if (m_LogLevel >= LogLevelInfo)
			std::cout << "[INFO]: " << Message << std::endl;
	}
thanks for setting me straight

I've been coding with VB.Net for along time and it seems a little foreign to define the class structure separate from the actual code that is doing the work.

I'm going to have to change my view of the world a little.
Topic archived. No new replies allowed.