2

I am compiling a logging program, but I am receiving this error and cant figure it out for the life of me...

logger.cpp:15: error: redefinition of ‘class Logger’
logger.h:20: error: previous definition of ‘class Logger’

with gcc when i compile with

g++ -Wall logger.cpp -o log

logger.h:

#ifndef LOGGER_H
#define LOGGER_H

#include <fstream>
#include <iostream>
#include <string>
using std::string;

class Logger
{

static Logger* m_pInstance;

public:
static Logger* Instance() { return m_pInstance; }
void writeLog(string message);
void openLogFile(string fileName);
void closeLogFile();
void deleteLogger();

};
#endif

logger.cpp

#include "logger.h"

#include <fstream>
#include <iostream>

class Logger
{
static Logger* m_pInstance;
std::ofstream m_pOutputFile;
Logger()
{
}
~Logger()
{
}

public:
static Logger* Instance()
{
    if(!m_pInstance)
    {
        m_pInstance = new Logger;
    }
    return m_pInstance;
}
void writeLog(std::string message)
{
    m_pOutputFile << message << "\n";
    std::cout << "you just wrote  " << message << "  to the log file!\n" << std::endl;
}
void openLogFile(std::string fileName)
{
    m_pOutputFile.open(fileName.c_str(),std::ios::out);
}
void closeLogFile()
{
    m_pOutputFile.close();
}
void deleteLogger()
{
    delete m_pInstance;
}
};
Logger* Logger::m_pInstance = NULL;
Andrew L
  • 534
  • 1
  • 5
  • 9

3 Answers3

5

It's exactly what the error message says. The implementation file can't just provide a redefinition of the class adding new member variables and conflicting function bodies wherever it pleases. Instead, provide definitions for the functions and static member variables you've already declared.

#include "logger.h"

#include <fstream>
#include <iostream>


static Logger::Logger* m_pInstance;

Logger::Logger()
{
}

Logger::~Logger()
{
}

// this also is illegal, there's a body provided in the header file
//Logger* Logger::Instance()
//{
//    if(!m_pInstance)
//    {
//        m_pInstance = new Logger;
//    }
//    return m_pInstance;
//}

void Logger::writeLog(std::string message)
{
    m_pOutputFile << message << "\n";
    std::cout << "you just wrote  " << message << "  to the log file!\n" << std::endl;
}

and so on

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Okay i made the changes but now i receive this error for Logger() and ~Logger().............. logger.cpp:17: error: definition of implicitly-declared ‘Logger::Logger()’ logger.cpp:17: error: declaration of ‘Logger::Logger()’ throws different exceptions logger.h:18: error: from previous declaration ‘Logger::Logger() throw ()’ – Andrew L Feb 04 '11 at 21:46
  • 1
    Of course the implementation in logger.cpp needs to have the same signature as in the header, the `throw ()` is also needed there. Also, don't duplicate any function or ctor body you already implemented in the logger.h file, only implement it in one. – Xeo Feb 04 '11 at 22:22
1

Well, because you are redefining the class. You can't say 'class Logger {' again in the .cpp when you already included it from the .h.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
0

Compiler always expects only one class definition in the whole namespace(or scope) that class belongs to. Currently in the code you specified, you would see that there are infact 2 class definitions: one in .h file and another one in .cpp file. That is why the compiler is complaining that you are redefining a class which is not allowed.

Generally whenever you encounter a compiler error, it's a good idea to look at the lines that compiler tells. Most of the time the problem is in the line the compiler points out.

VNarasimhaM
  • 1,460
  • 3
  • 22
  • 36