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
QtWS25 Last Chance

Invalid operands to binary expresion

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 4.2k Views
  • 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 15 May 2019, 22:54 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??

    J 1 Reply Last reply 16 May 2019, 04:32
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 15 May 2019, 23:48 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 16 May 2019, 19:49
      2
      • A arsinte_andrei
        15 May 2019, 22:54

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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 16 May 2019, 04:32 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 16 May 2019, 19:47
        4
        • J jsulm
          16 May 2019, 04:32

          @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 16 May 2019, 19:47 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
          • K Kent-Dorfman
            15 May 2019, 23:48

            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 16 May 2019, 19:49 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

            K 1 Reply Last reply 16 May 2019, 20:54
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 May 2019, 19:53 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 16 May 2019, 20:36
              2
              • S SGaist
                16 May 2019, 19:53

                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 16 May 2019, 20:36 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...

                K 1 Reply Last reply 16 May 2019, 20:56
                1
                • A arsinte_andrei
                  16 May 2019, 19:49

                  @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

                  K Offline
                  K Offline
                  Kent-Dorfman
                  wrote on 16 May 2019, 20:54 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 16 May 2019, 22:22
                  0
                  • A arsinte_andrei
                    16 May 2019, 20:36

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

                    K Offline
                    K Offline
                    Kent-Dorfman
                    wrote on 16 May 2019, 20:56 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
                    • K Kent-Dorfman
                      16 May 2019, 20:54

                      @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 16 May 2019, 22:22 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

                      8/10

                      16 May 2019, 20:54

                      • Login

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