Results 1 to 8 of 8

Thread: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    Hello!

    I would like to be able to serialize my custom vector class to a QDataStream, and for some reason that I don't understand, my operator overloading doesn't work.
    Here is the relevant part of my vector class:

    Qt Code:
    1. #include <QDataStream>
    2.  
    3. template <int N=3>
    4. class NVec
    5. {
    6. double v[N];
    7.  
    8. public:
    9.  
    10. NVec() {memset(v, 0, N*sizeof(double));}
    11. };
    12.  
    13. template <int N>
    14. QDataStream& operator<<(QDataStream& out, const NVec<N> &o)
    15. {
    16. for (int i = 0; i < N; i++)
    17. out << o[i];
    18. return out;
    19. }
    20.  
    21. template <int N>
    22. QDataStream& operator>>(QDataStream& in, const NVec<N> &o)
    23. {
    24. for (int i = 0; i < N; i++)
    25. in >> o[i];
    26. return in;
    27. }
    To copy to clipboard, switch view to plain text mode 

    The compiler complains with

    no match for 'operator>>' (operand types are 'QDataStream' and 'double')
    even though I am certain that I have used QDataStream with doubles before. It is also strange that only the read operator fails, but not the write operator.
    Can anyone please point out where I went wrong?

    Thanks
    Cruz

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    For operator>>(), the NVec<N> argument has to be non-const. (You're modifying it after all).

    Cut-n-paste strikes again, eh?

  3. The following user says thank you to d_stranz for this useful post:

    Cruz (8th December 2015)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    Oh crap. I am the stupidest stupid in the world.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    Nope, I've done it too. I was stupid before you were.

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    You _were_, but now I have to wait for someone else to make that mistake in order to pass on the stupid.

  7. The following user says thank you to Cruz for this useful post:

    d_stranz (10th December 2015)

  8. #6
    Join Date
    Jan 2016
    Location
    Finland
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    Yeap, I've come to collect the stupid Feel free to pass it on
    I have a similar problem here except the problem persists even after the argument was changed to a non-const. Any idea as to why that might happen? Below is the implementation of the two operators.

    Qt Code:
    1. QDataStream &operator <<(QDataStream &out, const PID &pid)
    2. {
    3. out << pid.getKp() << pid.getKi() << pid.getKd();
    4. return out;
    5. }
    6.  
    7.  
    8. QDataStream &operator >>(QDataStream &in, PID &pid)
    9. {
    10. in >> pid.getKp() >> pid.getKi() >> pid.getKd();
    11. return in;
    12. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    Do the getter functions, e.g. PID.getKp(), return a reference to which the >> operator could write?

    Cheers,
    _

  10. #8
    Join Date
    Jan 2016
    Location
    Finland
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')

    Yes that was the problem. Thank you. I changed it to the code below and now it's okay. I guess I'll wear my cap for while Thanks again.
    Qt Code:
    1. QDataStream &operator >>(QDataStream &in, PID &pid)
    2. {
    3. double kp;
    4. double ki;
    5. double kd;
    6. in >> kp >> ki >> kd;
    7. pid.setKp(kp);
    8. pid.setKi(ki);
    9. pid.setKd(kd);
    10. return in;
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 29th January 2014, 14:23
  2. no match fo 'operator[]='
    By jeff28 in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2012, 09:53
  3. no match for 'operator=' in...
    By toss in forum Newbie
    Replies: 2
    Last Post: 14th April 2010, 00:08
  4. Qt QDataStream supports data types
    By LoginFailed in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2009, 08:41
  5. No match for operator>>
    By Salazaar in forum Newbie
    Replies: 18
    Last Post: 12th June 2007, 17:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.