C++类重定义错误

发布于 2024-10-16 07:43:55 字数 1447 浏览 6 评论 0原文

我正在编译一个日志程序,但是我收到了这个错误,并且无法弄清楚我的一生......

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

当我使用 logger.h 进行编译时,使用 gcc

g++ -Wall logger.cpp -o log

#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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2024-10-23 07:43:55

这正是错误消息所说的。实现文件不能只是提供类的重新定义,在任意位置添加新的成员变量和冲突的函数体。相反,为已声明的函数和静态成员变量提供定义。

#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;
}

等等

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

任谁 2024-10-23 07:43:55

好吧,因为你正在重新定义类。当您已经从 .h 中包含“class Logger {”时,您不能在 .cpp 中再次说出“class Logger {”。

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.

百思不得你姐 2024-10-23 07:43:55

编译器始终期望该类所属的整个命名空间(或范围)中只有一个类定义。当前在您指定的代码中,您会看到实际上有 2 个类定义:一个在 .h 文件中,另一个在 .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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文