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. overload QTextStream::operator<<
Forum Updated to NodeBB v4.3 + New Features

overload QTextStream::operator<<

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 269 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.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by
    #1

    I want to overload operator <<. How I can do it? I found similar topic, but my code produce error

    #include <QTextStream>
    
    QTextStream &operator<<(QTextStream& out, const QString& string)
    {
        return out << string << ";";
    }
    
    MainClass::MainClass(QObject *parent)
        : QObject(parent)
    {
        QString str;
        QTextStream s(&str);
        QString p("text");
        s<< p;
        qDebug() << str;
    }
    

    error: ambiguous overload for ‘operator<<’ (operand types are ‘QTextStream’ and ‘const QString’)

    Christian EhrlicherC 1 Reply Last reply
    0
    • D DungeonLords

      I want to overload operator <<. How I can do it? I found similar topic, but my code produce error

      #include <QTextStream>
      
      QTextStream &operator<<(QTextStream& out, const QString& string)
      {
          return out << string << ";";
      }
      
      MainClass::MainClass(QObject *parent)
          : QObject(parent)
      {
          QString str;
          QTextStream s(&str);
          QString p("text");
          s<< p;
          qDebug() << str;
      }
      

      error: ambiguous overload for ‘operator<<’ (operand types are ‘QTextStream’ and ‘const QString’)

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @DungeonLords said in overload QTextStream::operator<<:

      QTextStream &operator<<(QTextStream& out, const QString& string)

      This function already exists, you can't overload it again.

      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
      1
      • Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @DungeonLords to add to @Christian-Ehrlicher :
        ...and because it's not virtual, you can't override this operator outside of the QTextStream class.

        What you can do:
        Subclass it and make your own stream class based on QTextStream. There you can define your own << operator


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1

        • Login

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