Qt6 topics merged with General

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


    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