C++类重定义错误
我正在编译一个日志程序,但是我收到了这个错误,并且无法弄清楚我的一生......
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是错误消息所说的。实现文件不能只是提供类的重新定义,在任意位置添加新的成员变量和冲突的函数体。相反,为已声明的函数和静态成员变量提供定义。
等等
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.
and so on
好吧,因为你正在重新定义类。当您已经从 .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.
编译器始终期望该类所属的整个命名空间(或范围)中只有一个类定义。当前在您指定的代码中,您会看到实际上有 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.