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

operator<< has too many parameters



  • I'm trying to override the operator << in order to implement a kind of to string using QDebug and for other usage cases, the problem is that I'm getting an error:

    QDataStream& operator<<(QDataStream &stream, const Namer &namer)
    {
        return stream << "hi";
    }
    
    Namer namer;
    qDebug() << namer;
    

    binary 'operator <<' has too many parameters
    binary '<<' : no operator found which takes a left-hand operand of type 'QDebug' (or there is no acceptable conversion) ...


  • Lifetime Qt Champion

    Hi,

    If you would like to send your class through QDebug you need to implement the stream operator for QDebug i.e.:

    QDebug operator <<(QDebug dbg, const Name& namer)
    {
    dbg << namer.myNiceMethod();
    return dbg;
    }
    


  • @SGaist It's not only for QDebug, but in cases of strings. I thought that QDataStream would do the job.


  • Lifetime Qt Champion

    If you want to support several streams then you have to implement them separately.

    As for strings don't you mean QTextStream ?



  • You are right, I checked a few sources and now it's working, thank you.


Log in to reply