Invalid operands to binary expresion



  • this is the error when I'm trying to compile this... Why do I get it?? What do I do wrong?

    the header file

    #ifndef ATPDATATYPES_H
    #define ATPDATATYPES_H
    
    #include <QMap>
    #include <QString>
    #include <QDataStream>
    
    struct atpMessage {
    //		atpMessage() {} //TODO atpMessage
    		QString userId;
    		QString userPassword;
    		QString device;
    		QString module;
    		QList<QMap<QString, QString>> tblData;
    };
    
    QDataStream &operator<<(QDataStream &out, const atpMessage &mesaj) {
    	out << mesaj.userId << mesaj.userPassword << mesaj.device << mesaj.module << mesaj.tblData;
    	return out;
    }
    
    QDataStream &operator>>(QDataStream &in, atpMessage &mesaj) {
    
    	in >> mesaj.userId >>  mesaj.userPassword >> mesaj.device >> mesaj.module >> mesaj.tblData;
    //	movie.id = (int)id;
    //	movie.releaseDate = date;
    	return in;
    }
    
    
    #endif // ATPDATATYPES_H
    
    

    somewhere in the program that is including the header file

    QMap<QString, QString> testMap1;
    	atpTcpMesaj.device = "Raspberry Pi";
    	atpTcpMesaj.module = "AlarmClock";
    	atpTcpMesaj.userId = "andrei";
    	atpTcpMesaj.userPassword = "parola";
    	atpTcpMesaj.tblData.append(testMap1);
    
    	qDebug() << atpTcpMesaj; // that is the line that give the error....  Invalid operands to binary expresion
    
    /////
    atpwidgetalarmclock.cpp:28: error: no match for ‘operator<<’ (operand types are ‘QDebug’ and ‘atpMessage’)
      qDebug() << atpTcpMesaj;
    

    what it can be?? how to make my struct qDebug-able??



  • add a text format method to your atpMessage class, that returns a QString. QDebug won't know how to output adhoc classes. You need to provide the formatted string containing the relevant information about the object.


  • Qt Champions 2018

    @arsinte_andrei See https://doc.qt.io/qt-5/qdebug.html
    "Writing Custom Types to a Stream" chapter.
    You need to overload operator<<



  • @jsulm if you look carefully this is what I've done in my opinion... but how to do it properly??

    QDataStream &operator<<(QDataStream &out, const atpMessage &mesaj)
    

    this is for the << operator!?!?



  • @Kent-Dorfman if I return everything in QString then when I will use the operator << it won't work - and I need it to work in order to be able to send it as a stream over a TCP network


  • Lifetime Qt Champion

    Hi,

    You implemented the QDataStream operator, what @jsulm wrote (that can be found in the documentation he linked) is that you have to implement the QDebug stream operator for your class.



  • @SGaist & @jsulm

    Many thanks... after looking a bit more in depth I've just understood how the things have to be done...

    QDebug operator<<(QDebug debug, const atpMessage &mesaj) {
    	QDebugStateSaver saver(debug);
    	debug.nospace() << '(' << mesaj.userId << ", " << mesaj.userPassword << ", " << mesaj.device << ", " << mesaj.module << ", " << mesaj.tblData << ')';
    	return debug;
    }
    

    that solved my problem...
    Many thanks...



  • @arsinte_andrei said in Invalid operands to binary expresion:

    f I return everything in QString then when I will use the operator << it won't work - and I need it to work in order to be able to send it as a stream over a TCP network

    you misunderstand.

    If you write a QString atpMessage::toString() {} method then qdebug << message.toString() will work, or, as @jsulm wrote you can overload the operator<<() for your class and QDebug will be able to handle it natively. Either will work, but I prefer my classes to use toString() as I don't like overloading << for types other than where the target is std::ostream...I know...I know...very java-esque.



  • @arsinte_andrei said in Invalid operands to binary expresion:

    Many thanks... after looking a bit more in depth I've just understood how the things have to be done...

    incidentally, if you go with overloading << then by all mean, pass QDebug by reference!



  • @Kent-Dorfman I've understood your point but unfortunately for me, to have a function that will return strings will be un proper and also not a Qt style... I work with Qt so I want to do it Qt like... In Qt, if you have a type .toString then you will find also .fromString (or number in case of QString). Thanks to anyway... I've sorted it out ..


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.