Solved QTextStream interpreted constant by gcc
-
Hello everyone. I'm new on Qt, and I'm working on Ubuntu 18 Qt5.15.2. I use gcc for debogging.
As part of my project, I wanted to develop my own logger. In this context, I create a Loggin object, which instantiate a LoggerLog object, witch record in real time data in a log file. The Loggin object create e LoggerTerminal object too, which displays with coloring in real time the equivalent in the terminal.These 2 instances inherit from a virtual class named Logger.
In the write method of LoggerLog, I use a QTextSteam created by my constructor. This QTextStream is itself defined as a variable and not a constant. However, when debugging, I see that the compiler defines it as a constant, which prevents me from using it beyond the constructor.
Sorry, it's a lot of files extracts, but I simplified them to the maximum.
logger.h
#ifndef LOGGER_H #define LOGGER_H //Qt += core #include <QString> //SL #include <string> enum importance_level: quint8 {critical, v_important, important, normal}; enum type_event: quint8 {error, alert, success, failure, instruction}; class Logger { public: virtual ~Logger(){} virtual void write( const QString&, const importance_level level = importance_level::normal, const type_event type = type_event::instruction) const = 0; virtual QString get_level(importance_level level) const { switch(level) { case importance_level::normal: return ""; case importance_level::important: return "IMPORTANT: "; } } virtual QString get_type(type_event type) const { switch(type) { case type_event::instruction: return "INSTRUCTION: "; case type_event::failure: return "FAILURE: "; } } protected: const QString m_T_format = "hh:mm:ss"; const QString m_DT_format = "ddd d MMMM yyyy hh:mm:ss"; }; #endif // LOGGER_H
loggerLog.h
#ifndef LOGGERLOG_H #define LOGGERLOG_H // QT += core #include <QtCore> #include <QFile> #include <QTextStream> #include <QDataStream> //virtual class #include "logger.h" class LoggerLog : public Logger { public: LoggerLog(const QString name_logFile); virtual ~LoggerLog(); virtual void write( const QString& msg, const importance_level level = importance_level::normal, const type_event type = type_event::instruction) const; private: QFile m_logFile; QTextStream m_out; }; #endif //LOGGERLOG_H
loggerLog.cpp
#include "loggerLog.h" LoggerLog::LoggerLog(const QString name_logFile) : m_logFile(name_logFile), m_out(&m_logFile) { m_logFile.open(QFile::WriteOnly | QFile::Truncate); m_out << " =============================================== \n" << " Begin Output log :\n" << " - " << QDateTime::currentDateTime().toString(m_DT_format) << '\n' << " =============================================== \n\n"; } LoggerLog::~LoggerLog() { m_out << '\n' << " =============================================== \n" << " End Output log :\n" << " - " << QDateTime::currentDateTime().toString(m_DT_format) << '\n' << " =============================================== \n\n"; } void LoggerLog::write( const QString& msg, const importance_level level, const type_event type) const { m_out << ' ' << QTime::currentTime().toString(m_T_format) << ": - " << get_level(level) << get_type(type) << msg << '\n'; }
my terminal output :
$ ./compile_logger.bash ../../src/log/loggerLog.cpp: In member function ‘virtual void LoggerLog::write(const QString&, importance_level, type_event) const’: ../../src/log/loggerLog.cpp:42:13: error: no match for ‘operator<<’ (operand types are ‘const QTextStream’ and ‘QString’) m_out << QTime::currentTime().toString(m_T_format) << ": - " ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you have any idea of the reason of this interpretation, or a solution to avoid this QTextStream to pass constant, it would be very nice. Thank's in advance !!
Champax
-
Your write() function is const so the members are const too - basic c++. Make you function non-const.
-
Your write() function is const so the members are const too - basic c++. Make you function non-const.
-
@champax said in QTextStream interpreted constant by gcc:
void LoggerLog::write( const QString& msg,
const importance_level level,
const type_event type) constYour method is
const
but you are trying to modify a member variablem_out
inside of it.Change
write()
to be non-const and it will work.