Results 1 to 3 of 3

Thread: QDataSteam.

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QDataSteam.

    I want write a class which is inherit from QObject to the file with QDataStream, so I overload operator << and operator >>.
    Qt Code:
    1. Class C : public QObject
    2. {
    3. Q_OBJECT
    4. ......
    5. friend QDataStream & operator << (QDataStream &dataStream, C &c);
    6. friend QDataStream & operator >> (QDataStream &dataStream, C &C);
    7. .......
    8. }
    9.  
    10. .................
    11. QDataStream & operator << (QDataStream &dataStream, C &c)
    12. {
    13.  
    14. }
    15.  
    16. QDataStream & operator >> (QDataStream &dataStream, C &c)
    17. {
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    but it can't compile success.The error is:multiple definition of "operator" <<(QDataStream&,C&)
    multiple definition of "operator" >>(QDataStream&,C&)

    if this class is not inherit from the QObject and delete macro Q_OBJECT, then it will compile success. So What's the problem....?
    Last edited by wysota; 13th September 2010 at 15:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QDataSteam.

    Obviously you can not have the same function or operator twice.
    Your code shows that you implemented the >> operator twice (the same).

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataSteam.

    Bodies of the operators should be moved to a .cpp file. If you place them in a header file, they will be defined each time the file is included somewhere and you'll be getting multiple definitions. An alternative is to declare those functions as inline.

    By the way, the << operator should be getting a const object. Otherwise you won't be able to use the operator in some contexts (i.e. some const methods).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Tags for this Thread

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.