Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with operator <<



  • 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!


  • Moderators

    @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;
    

  • Moderators

    @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



  • @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;

  • Moderators

    @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;
    

Log in to reply