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. Overloading QDataStream '<<' and '>>' operator help
Forum Updated to NodeBB v4.3 + New Features

Overloading QDataStream '<<' and '>>' operator help

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.6k Views 1 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.
  • F Offline
    F Offline
    Fwomp
    wrote on 19 Dec 2017, 00:02 last edited by
    #1

    Hello all!

    Was hoping somebody could help me with an issue I've run into regarding the overloading of the '>>' and '<<' operators for use with a custom class and QDataStream. I'm trying to create a class and then setup overloaded operators for streaming with a QDataStream, but am getting compilation errors. I've attached a simplified version of the code I'm trying to get working along with the error message below.

    Thanks!

    main.cpp:

    #include "class1.h"
    #include <QCoreApplication>
    #include <QDataStream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // No datastream has been setup, this is yet to be implemented.
        QDataStream myStream;
        class1 myClass;
    
        myClass << myStream;
    
        return a.exec();
    }
    

    class1.cpp:

    #include "class1.h"
    
    class1::class1(QObject *parent) : QObject(parent)
    {
    
    }
    
    QDataStream & operator>> (QDataStream& stream, class1& image)
    {
        stream >> image.myString;
        return stream;
    }
    
    QDataStream & operator<< (QDataStream& stream, const class1& image)
    {
        stream << image.myString;
        return stream;
    }
    

    class1.h

    #ifndef CLASS1_H
    #define CLASS1_H
    
    #include <QDataStream>
    #include <QObject>
    #include <QString>
    
    class class1 : public QObject
    {
        Q_OBJECT
    public:
        explicit class1(QObject *parent = nullptr);
    
        friend QDataStream & operator>> (QDataStream& stream, class1& image);
        friend QDataStream & operator<< (QDataStream& stream, const class1& image);
    
    private:
        QString myString = "default";
    };
    
    #endif // CLASS1_H
    

    error:

    main.cpp:13: error: no match for 'operator<<' (operand types are 'class1' and 'QDataStream')
         myClass  << myStream;
                  ^
    
    J 1 Reply Last reply 19 Dec 2017, 01:06
    0
    • F Fwomp
      19 Dec 2017, 00:02

      Hello all!

      Was hoping somebody could help me with an issue I've run into regarding the overloading of the '>>' and '<<' operators for use with a custom class and QDataStream. I'm trying to create a class and then setup overloaded operators for streaming with a QDataStream, but am getting compilation errors. I've attached a simplified version of the code I'm trying to get working along with the error message below.

      Thanks!

      main.cpp:

      #include "class1.h"
      #include <QCoreApplication>
      #include <QDataStream>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          // No datastream has been setup, this is yet to be implemented.
          QDataStream myStream;
          class1 myClass;
      
          myClass << myStream;
      
          return a.exec();
      }
      

      class1.cpp:

      #include "class1.h"
      
      class1::class1(QObject *parent) : QObject(parent)
      {
      
      }
      
      QDataStream & operator>> (QDataStream& stream, class1& image)
      {
          stream >> image.myString;
          return stream;
      }
      
      QDataStream & operator<< (QDataStream& stream, const class1& image)
      {
          stream << image.myString;
          return stream;
      }
      

      class1.h

      #ifndef CLASS1_H
      #define CLASS1_H
      
      #include <QDataStream>
      #include <QObject>
      #include <QString>
      
      class class1 : public QObject
      {
          Q_OBJECT
      public:
          explicit class1(QObject *parent = nullptr);
      
          friend QDataStream & operator>> (QDataStream& stream, class1& image);
          friend QDataStream & operator<< (QDataStream& stream, const class1& image);
      
      private:
          QString myString = "default";
      };
      
      #endif // CLASS1_H
      

      error:

      main.cpp:13: error: no match for 'operator<<' (operand types are 'class1' and 'QDataStream')
           myClass  << myStream;
                    ^
      
      J Offline
      J Offline
      joeQ
      wrote on 19 Dec 2017, 01:06 last edited by joeQ
      #2

      @Fwomp Hi, friend. Welcome.

      Your code's syntax was wrong.

      QDataStream myStream;
          class1 myClass;
      
          ///< myClass << myStream; ///< Its wrong.
         myStream >> myClass; /// stream => myClass,
         myStream << myClass; /// myClass => stream, 
      

      Because, you defined

      friend QDataStream & operator>> (QDataStream& stream, class1& image); ///< stream is first arguments, class is second argument
      friend QDataStream & operator<< (QDataStream& stream, const class1& image);
      

      if you defined like below:

          friend QDataStream & operator>> (class1& image,QDataStream& stream); ///< first is class and second is stream.
          friend QDataStream & operator<< (const class1& image,QDataStream& stream);
      

      you will right:

      class1 >> stream;
      

      Just do it!

      F 1 Reply Last reply 19 Dec 2017, 01:09
      3
      • J joeQ
        19 Dec 2017, 01:06

        @Fwomp Hi, friend. Welcome.

        Your code's syntax was wrong.

        QDataStream myStream;
            class1 myClass;
        
            ///< myClass << myStream; ///< Its wrong.
           myStream >> myClass; /// stream => myClass,
           myStream << myClass; /// myClass => stream, 
        

        Because, you defined

        friend QDataStream & operator>> (QDataStream& stream, class1& image); ///< stream is first arguments, class is second argument
        friend QDataStream & operator<< (QDataStream& stream, const class1& image);
        

        if you defined like below:

            friend QDataStream & operator>> (class1& image,QDataStream& stream); ///< first is class and second is stream.
            friend QDataStream & operator<< (const class1& image,QDataStream& stream);
        

        you will right:

        class1 >> stream;
        
        F Offline
        F Offline
        Fwomp
        wrote on 19 Dec 2017, 01:09 last edited by
        #3

        @joeQ Thanks that's solved it!

        J 1 Reply Last reply 19 Dec 2017, 01:13
        0
        • F Fwomp
          19 Dec 2017, 01:09

          @joeQ Thanks that's solved it!

          J Offline
          J Offline
          joeQ
          wrote on 19 Dec 2017, 01:13 last edited by
          #4

          @Fwomp I modified my answer.

          Just do it!

          1 Reply Last reply
          0
          • D DungeonLords referenced this topic on 12 Sept 2024, 16:30

          1/4

          19 Dec 2017, 00:02

          • Login

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