Results 1 to 2 of 2

Thread: QDataStream class/struct & stream operators

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question QDataStream class/struct & stream operators

    Hi everybody,

    following problem:

    I'm using a struct comparable to this one in my code:
    Qt Code:
    1. #ifndef STRUCT_MYPOINT
    2. #define STRUCT_MYPOINT
    3. #include <QDataStream>
    4.  
    5. typedef struct MyPoint {
    6. int x;
    7. int y;
    8. }MyPoint;
    9.  
    10. QDataStream &operator<<(QDataStream &out, const MyPoint &p)
    11. {
    12. out << (qint32)p.x;
    13. out << (qint32)p.y;
    14. return out;
    15. }
    16.  
    17. QDataStream &operator>>(QDataStream &in, MyPoint &p)
    18. {
    19. in >> (qint32)p.x;
    20. in >> (qint32)p.y;
    21. return in;
    22. }
    23. #endif
    To copy to clipboard, switch view to plain text mode 
    this struct is defined in a seperate *.h file, lets say "mypoint.h"

    my code works fine if I compile it with Visual Studio .Net & Qt 4.4.0

    now I also try to compile my code under a linux environment using qt 4.4.0 and gcc 4.2.0

    now the same code won't compile anymore.
    I get a long list of errors messages, with the important ones saying

    no match for 'operator>>'
    no match for 'operator<<'

    searched the net for some kind of solution, but haven't found one.
    only thing I learned is that I must add, at least, the following lines to my code:
    Qt Code:
    1. friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const MyPoint &);
    2. friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, MyPoint &);
    To copy to clipboard, switch view to plain text mode 

    added these lines to my struct:

    Qt Code:
    1. #ifndef STRUCT_MYPOINT
    2. #define STRUCT_MYPOINT
    3. #include <QDataStream>
    4.  
    5. typedef struct MyPoint {
    6. int x;
    7. int y;
    8. }MyPoint;
    9.  
    10. QDataStream &operator<<(QDataStream &out, const MyPoint &p)
    11. {
    12. out << (qint32)p.x;
    13. out << (qint32)p.y;
    14. return out;
    15. }
    16.  
    17. QDataStream &operator>>(QDataStream &in, MyPoint &p)
    18. {
    19. in >> (qint32)p.x;
    20. in >> (qint32)p.y;
    21. return in;
    22. }
    23. friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const MyPoint &);
    24. friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, MyPoint &);
    25. #endif
    To copy to clipboard, switch view to plain text mode 

    but the problem remains.

    So what am I missing/doing wrong???

    greetz
    darksaga

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream class/struct & stream operators

    side note: the typedef around the struct is not necessary in C++.

    Qt Code:
    1. QDataStream &operator>>(QDataStream &in, MyPoint &p)
    2. {
    3. in >> (qint32)p.x;
    4. in >> (qint32)p.y;
    5. return in;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I would assume that you can't cast p.x to a value and stream into this value.
    Probably something like
    Qt Code:
    1. qint32 tmp;
    2. in >> tmp;
    3. p.x = tmp;
    To copy to clipboard, switch view to plain text mode 
    would be more appropriate.

    Apart from that:
    Please show us the code that produces the error messages and the complete error message, too.


    And finally,
    I suggest to just declare the output function in the header (and implement them in some .cpp file). Otherwise you will end up with multiple definitions of those functions and get linker errors.

    HTH

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.