Results 1 to 2 of 2

Thread: SOLVED: Operator overloading QDataStream

  1. #1
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default SOLVED: Operator overloading QDataStream

    Quick update:
    After I wrote this post i tried something i didn't do before. I had a working overload in another class that worked so and the only difference was that the definition was in the header file and the actual function was in the .cpp file. When i tested what happend when i moved the code to the header file i got the same error, so apparently you cant write the code of the overloaded function in the header file. When i applied this to my problem the errors went away.



    Hello there,

    I am trying to overload the << operator of QDataStream so I can use it with one of my own classes. Now the first problem that i have is that the classes are inherited classes.

    I have AbstractPostItem which is the base class and PostDataItem and PostHeaderItem that inherited AbstractPostItem.

    I first tried to implement the overloaded function in all the classes but that didn't work. I know have one overloaded function in AbstractPostItem that looks like this:

    Qt Code:
    1. QDataStream& operator<<(QDataStream& out, const AbstractPostItem& outputItem)
    2. {
    3. return outputItem.saveData(out);
    4. }
    To copy to clipboard, switch view to plain text mode 

    It calls saveData which looks like this:
    Qt Code:
    1. virtual QDataStream& saveData(QDataStream& out) const
    2. {
    3. out << childItems;
    4. return out;
    5. }
    To copy to clipboard, switch view to plain text mode 

    This function is implemented in all the classes.

    But know when i try to compile the code i get some strange error:
    Qt Code:
    1. mingw32-make: Leaving directory `G:/projects/Post_Program'
    2. ./debug/mainwindow.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    3. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/arch/qatomic_i386.h:125: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    4. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    5. ./debug/main.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    6. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/arch/qatomic_i386.h:125: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    7. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    8. ./debug/newpost.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    9. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    10. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    11. ./debug/postmodel.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    12. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    13. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    14. ./debug/postheaderitem.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    15. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    16. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    17. ./debug/postdataitem.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    18. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    19. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    20. ./debug/moc_postview.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    21. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    22. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    23. ./debug/moc_mainwindow.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    24. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    25. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    26. ./debug/moc_newpost.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    27. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    28. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    29. ./debug/moc_postmodel.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
    30. e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
    31. ./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
    32. collect2: ld returned 1 exit status
    33. mingw32-make[1]: *** [debug/Post_Program.exe] Error 1
    34. mingw32-make: *** [debug] Error 2
    35. Exited with code 2.
    36. Error while building project Post_Program (target: Desktop)
    37. When executing build step ''
    To copy to clipboard, switch view to plain text mode 

    It is talking about multiple definitions but i cant find the multiple definition, when i look on line 1368 in qglobal is says :
    Qt Code:
    1. inline void qt_noop() {}
    To copy to clipboard, switch view to plain text mode 

    So i have no idea where my multiple definition is.

    Hope you guys can help me.

    Marcel
    Attached Images Attached Images
    Last edited by eekhoorn12; 9th May 2010 at 23:19. Reason: Solved

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: SOLVED: Operator overloading QDataStream

    Hard to say without seeing your code in it's entirety, but usually a multiple definition error crops up when you forget to put guard statements around your header file contents. The file then gets included in a compilation unit more than once, causing the problems. This can never happen with your implementation files, because they don't get included by other files and are only seen by the compiler once.

    Do something like this in all of your *.h files:

    Qt Code:
    1. #ifndef MY_UNIQUE_SYMBOL_FOR_THIS_FILE
    2. #define MY_UNIQUE_SYMBOL_FOR_THIS_FILE
    3.  
    4. // Put whatever normally goes in your header file here, as usual
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    and see if the problem goes away.

Similar Threads

  1. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28
  2. overloading global "new" operator
    By mickey in forum General Programming
    Replies: 0
    Last Post: 10th April 2008, 17:01
  3. operator [] overloading
    By darksaga in forum General Programming
    Replies: 5
    Last Post: 8th April 2008, 15:27
  4. Simple: Operator overloading with heap objects
    By durbrak in forum General Programming
    Replies: 12
    Last Post: 25th April 2007, 13:20
  5. Replies: 13
    Last Post: 19th May 2006, 02:10

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.