おはようございます、
私は現在、Qt プロジェクト (使用するソフトウェア: VS 2010) に取り組んでいます。QLogger.cpp で、関数 addDestination に C2511 エラーがありますが、すべての関数が正しく使用されているため、理由がわかりません。デバッグモードでは動作しないため、リリースモードでコーディングしています。
ここに完全なメッセージエラーがあります:
エラー 1 エラー C2511: 'bool QLogger::QLoggerManager::addDestination(const QString &,const QString &,QLogger::LogLevel)': オーバーロードされたメンバー関数が 'QLogger::QLoggerManager' C:\Users\gdeca_000\Desktop に見つかりません\qt-coding\QLogger\QLogger.cpp 104
ここで.hコード:
static QString levelToText(const LogLevel &level);
/**
* @brief This method creates a QLoogerWriter that stores the name of the file and the log
* level assigned to it. Here is added to the map the different modules assigned to each
* log file. The method returns <em>false</em> if a module is configured to be stored in
* more than one file.
*
* @param fileDest The file name and path to print logs.
* @param modules The modules that will be stored in the file.
* @param level The maximum level allowed.
* @return Returns true if any error have been done.
*/
// HERE THE DEFINITION OF ADDDESTINATION
bool addDestination(const QString &fileDest, const QString &module, LogLevel level);
bool addDestination(const QString &fileDest, const QStringList &modules, LogLevel level);
// HERE THE DEFINITION OF ADDDESTINATION
/**
* @brief Gets the QLoggerWriter instance corresponding to the module <em>module</em>.
* @param module The module we look for.
* @return Retrns a pointer to the object.
*/
QLoggerWriter * getLogWriter(const QString &module) { return moduleDest.value(module); }
/**
* @brief This method closes the logger and the thread it represents.
*/
void closeLogger();
/**
* @brief Mutex to make the method thread-safe.
*/
QMutex mutex;
private:
/**
* @brief Instance of the class.
*/
static QLoggerManager *INSTANCE;
/**
* @brief Map that stores the module and the file it is assigned.
*/
QMap<QString,QLoggerWriter*> moduleDest;
/**
* @brief Default builder of the class. It starts the thread.
*/
QLoggerManager();
};
}
#endif // QLOGGER_H
ここで .cpp コード:
#include <QDir>
#include <QDateTime>
#include <QTextStream>
#include "QLogger.h"
namespace QLogger
{
void QLog_Trace(const QString &module, const QString &message)
{
QLog_(module, TraceLevel, message);
}
void QLog_Debug(const QString &module, const QString &message)
{
QLog_(module, DebugLevel, message);
}
void QLog_Info(const QString &module, const QString &message)
{
QLog_(module, InfoLevel, message);
}
void QLog_Warning(const QString &module, const QString &message)
{
QLog_(module, WarnLevel, message);
}
void QLog_Error(const QString &module, const QString &message)
{
QLog_(module, ErrorLevel, message);
}
void QLog_Fatal(const QString &module, const QString &message)
{
QLog_(module, FatalLevel, message);
}
void QLog_(const QString &module, LogLevel level, const QString &message)
{
QLoggerManager *manager = QLoggerManager::getInstance();
QMutexLocker(&manager->mutex);
QLoggerWriter *logWriter = manager->getLogWriter(module);
if (logWriter && logWriter->getLevel() <= level)
logWriter->write(module,message);
}
//QLoggerManager
QLoggerManager * QLoggerManager::INSTANCE = NULL;
QLoggerManager::QLoggerManager() : QThread(), mutex(QMutex::Recursive)
{
start();
}
QLoggerManager * QLoggerManager::getInstance()
{
if (!INSTANCE)
INSTANCE = new QLoggerManager();
return INSTANCE;
}
QString QLoggerManager::levelToText(const LogLevel &level)
{
switch (level)
{
case TraceLevel: return "Trace";
case DebugLevel: return "Debug";
case InfoLevel: return "Info";
case WarnLevel: return "Warning";
case ErrorLevel: return "Error";
case FatalLevel: return "Fatal";
default: return QString();
}
}
// bug error C2511
// Declaration in H
// bool addDestination (const QString &fileDest, const QString &module, LogLevel level);
bool QLoggerManager::addDestination(const QString &fileDest, const QString &module, LogLevel level)
{
QLoggerWriter *log;
if (!moduleDest.contains(module))
{
log = new QLoggerWriter(fileDest,level);
moduleDest.insert(module, log);
return true;
}
return false;
}
// bool addDestination (const QString &fileDest, const QStringList &modules, LogLevel level); // declaration in H
bool QLoggerManager::addDestination(const QString &fileDest, const QStringList &modules, LogLevel level)
{
QLoggerWriter *log;
foreach (QString module, modules)
{
if (!moduleDest.contains(module))
{
log = new QLoggerWriter(fileDest,level);
moduleDest.insert(module, log);
return true;
}
}
return false;
}
void QLoggerManager::closeLogger()
{
exit(0);
deleteLater();
}
QLoggerWriter::QLoggerWriter(const QString &fileDestination, LogLevel level)
{
m_fileDestination = fileDestination;
m_level = level;
}
void QLoggerWriter::write(const QString &module, const QString &message)
{
QString _fileName = m_fileDestination;
int MAX_SIZE = 1024 * 1024;
QDir dir(QDir::currentPath());
if (!dir.exists("logs"))
dir.mkdir("logs");
QFile file(_fileName);
QString toRemove = _fileName.section('.',-1);
QString fileNameAux = _fileName.left(_fileName.size() - toRemove.size()-1);
bool renamed = false;
QString newName = fileNameAux + "_%1__%2.log";
//Renomenem l'arxiu si està ple
if (file.size() >= MAX_SIZE)
{
//Creem un fixer nou
QDateTime currentTime = QDateTime::currentDateTime();
newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss"));
renamed = file.rename(_fileName, newName);
}
file.setFileName(_fileName);
if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
{
QTextStream out(&file);
QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz");
if (renamed)
out << QString("%1 - Previuous log %2\n").arg(dtFormat).arg(newName);
QString logLevel = QLoggerManager::levelToText(m_level);
QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message);
out << text;
file.close();
}
}
}
あなたの助けと時間をありがとう。