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. Error overloading >> operator
Forum Update on Monday, May 27th 2025

Error overloading >> operator

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 539 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.
  • DSpiderD Offline
    DSpiderD Offline
    DSpider
    wrote on last edited by
    #1

    Hello,

    I am trying to overload the >> operator in a class, like this:

    class Sample
    {
    private:
        quint8 tag;
        quint32 time;
        qint16 val1;
        qint16 val2;
        qint16 val3;
        qint16 val4;
    public:
        Sample();
    
        friend QDataStream &operator>> (QDataStream &in, Sample &samp)
        {
            in >> samp.tag >> samp.time >> samp.val1 >> samp.val2 >> samp.val3 >> samp.val4;
            return in;
        }
    };
    

    But I have a complain saying "invalid operand between Qdatastream and Qint8". I actually was doing the same in a struct instead of a class and it was working.
    Any idea ?

    Best

    KillerSmathK 1 Reply Last reply
    0
    • DSpiderD DSpider

      Hello,

      I am trying to overload the >> operator in a class, like this:

      class Sample
      {
      private:
          quint8 tag;
          quint32 time;
          qint16 val1;
          qint16 val2;
          qint16 val3;
          qint16 val4;
      public:
          Sample();
      
          friend QDataStream &operator>> (QDataStream &in, Sample &samp)
          {
              in >> samp.tag >> samp.time >> samp.val1 >> samp.val2 >> samp.val3 >> samp.val4;
              return in;
          }
      };
      

      But I have a complain saying "invalid operand between Qdatastream and Qint8". I actually was doing the same in a struct instead of a class and it was working.
      Any idea ?

      Best

      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @DSpider
      How are you using operator>> in your code ? Something similar to the below code ?

      QFile file("[File Path]");
      file.open(QIODevice::ReadOnly);
      QDataStream in(&file);
      Sample sample;
      in >> sample;
      

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      1
      • DSpiderD Offline
        DSpiderD Offline
        DSpider
        wrote on last edited by
        #3

        Hi @KillerSmath . Exactly. I was actually doing all of these using struct, but I want to do it as Class now.

        KillerSmathK 1 Reply Last reply
        0
        • DSpiderD DSpider

          Hi @KillerSmath . Exactly. I was actually doing all of these using struct, but I want to do it as Class now.

          KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by
          #4

          @DSpider
          I tested your code. The serialization is working well, so i recommend you to review your code.

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I would guess you're missing to include the QDataStream header.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            DSpiderD 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              I would guess you're missing to include the QDataStream header.

              DSpiderD Offline
              DSpiderD Offline
              DSpider
              wrote on last edited by
              #6

              @Christian-Ehrlicher . That was exactly the issue ! Thank you so much, did not expect this from the error message.

              1 Reply Last reply
              1

              • Login

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