简体   繁体   English

C ++类重新定义错误

[英]C++ class redefinition error

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 当我用gcc编译时

g++ -Wall logger.cpp -o log

logger.h: 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 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;

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 等等

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. 如果您已经从.h文件中添加了“ .class Logger {”,则无法在.cpp文件中再说一次。

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. 当前,在您指定的代码中,您会看到实际上有2个类定义:一个在.h文件中,另一个在.cpp文件中。 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. 在大多数情况下,问题出在编译器指出的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM