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. Problem with operator <<
QtWS25 Last Chance

Problem with operator <<

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 398 Views
  • 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.
  • SteMMoS Offline
    SteMMoS Offline
    SteMMo
    wrote on last edited by
    #1

    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.HilkJ 1 Reply Last reply
    0
    • SteMMoS SteMMo

      @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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by Christian Ehrlicher
      #4

      @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


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

      1 Reply Last reply
      4
      • SteMMoS SteMMo

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #2

        @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


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

        SteMMoS 1 Reply Last reply
        4
        • J.HilkJ 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

          SteMMoS Offline
          SteMMoS Offline
          SteMMo
          wrote on last edited by
          #3

          @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.HilkJ 1 Reply Last reply
          0
          • SteMMoS SteMMo

            @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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by Christian Ehrlicher
            #4

            @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


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

            1 Reply Last reply
            4

            • Login

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