Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Problem with operator <<

    General and Desktop
    2
    4
    140
    Loading More Posts
    • 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.
    • SteMMo
      SteMMo last edited by

      Hi all,
      I'm defining a simple custom class and I'd like to define a << operator to have a suitable debug output.
      The class is defined as:

      class Prenotazione : public QObject
      {
        Q_OBJECT 
        public:
          int _id;
          int _value;
      
          QDebug operator<<( QDebug dbg, const Prenotazione &myObject){
              dbg << "QQQ";
              return dbg;
          };
      

      The compiler says that 'error C2804: 'operator <<' (binary) has too many parameters'.

      I saw many examples and they are written in this way .. in don't understand why I have this error ..

      Thanks!

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @SteMMo last edited by Christian Ehrlicher

        @SteMMo well, you overloaded it for a const reference, not for a pointer -> so dereference it

        p = new Prenotazione();
            p->_id = query.value("id").toUInt();
            qDebug()<< "[DataSource] p:" << *p;
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 4
        • J.Hilk
          J.Hilk Moderators @SteMMo last edited by J.Hilk

          @SteMMo friend is missing, you are trying to declare a non-member operator overload that acts on a class template for the QDebug << operator

          and not an overload for the Prenotazione << operator

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          SteMMo 1 Reply Last reply Reply Quote 4
          • SteMMo
            SteMMo @J.Hilk last edited by

            @J-Hilk thanks,
            the code now is accepted but I continue to have:

            [DataSource] p: Prenotazione(0x8098b118)
            

            from the code:

                p = new Prenotazione();
                p->_id = query.value("id").toUInt();
                qDebug()<< "[DataSource] p:" << p;
            
            J.Hilk 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @SteMMo last edited by Christian Ehrlicher

              @SteMMo well, you overloaded it for a const reference, not for a pointer -> so dereference it

              p = new Prenotazione();
                  p->_id = query.value("id").toUInt();
                  qDebug()<< "[DataSource] p:" << *p;
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 4
              • First post
                Last post