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. Invalid operands to binary expresion
Forum Updated to NodeBB v4.3 + New Features

Invalid operands to binary expresion

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 4.2k Views 3 Watching
  • 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.
  • A Offline
    A Offline
    arsinte_andrei
    wrote on last edited by
    #1

    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??

    jsulmJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      2
      • A arsinte_andrei

        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??

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        4
        • jsulmJ jsulm

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

          A Offline
          A Offline
          arsinte_andrei
          wrote on last edited by
          #4

          @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!?!?

          1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            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.

            A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #5

            @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

            Kent-DorfmanK 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              2
              • SGaistS SGaist

                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.

                A Offline
                A Offline
                arsinte_andrei
                wrote on last edited by
                #7

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

                Kent-DorfmanK 1 Reply Last reply
                1
                • A arsinte_andrei

                  @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

                  Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #8

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

                  A 1 Reply Last reply
                  0
                  • A arsinte_andrei

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

                    Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    1
                    • Kent-DorfmanK Kent-Dorfman

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

                      A Offline
                      A Offline
                      arsinte_andrei
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

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