Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextStream interpreted constant by gcc
Forum Updated to NodeBB v4.3 + New Features

QTextStream interpreted constant by gcc

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 176 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    champax
    wrote on 2 Jun 2021, 10:34 last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 2 Jun 2021, 10:38 last edited by
      #2

      Your write() function is const so the members are const too - basic c++. Make you function non-const.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 2 Jun 2021, 10:39 last edited by
        #3

        @champax said in QTextStream interpreted constant by gcc:

        void LoggerLog::write( const QString& msg,
        const importance_level level,
        const type_event type) const

        Your method is const but you are trying to modify a member variable m_out inside of it.

        Change write() to be non-const and it will work.

        (Z(:^

        1 Reply Last reply
        3

        1/3

        2 Jun 2021, 10:34

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved