PDA

View Full Version : SOLVED: Operator overloading QDataStream



eekhoorn12
9th May 2010, 23:06
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:



QDataStream& operator<<(QDataStream& out, const AbstractPostItem& outputItem)
{
return outputItem.saveData(out);
}

It calls saveData which looks like this:

virtual QDataStream& saveData(QDataStream& out) const
{
out << childItems;
return out;
}

This function is implemented in all the classes.

But know when i try to compile the code i get some strange error:

mingw32-make: Leaving directory `G:/projects/Post_Program'
./debug/mainwindow.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/arch/qatomic_i386.h:125: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/main.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/arch/qatomic_i386.h:125: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/newpost.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/postmodel.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/postheaderitem.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/postdataitem.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/moc_postview.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/moc_mainwindow.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/moc_newpost.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
./debug/moc_postmodel.o: In function `ZlsR11QDataStreamRK16AbstractPostItem':
e:/Qt/2010.01/qt/include/QtCore/../../src/corelib/global/qglobal.h:1368: multiple definition of `operator<<(QDataStream&, AbstractPostItem const&)'
./debug/postview.o:G:\projects\Post_Program/./abstractpostitem.h:100: first defined here
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug/Post_Program.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project Post_Program (target: Desktop)
When executing build step ''

It is talking about multiple definitions but i cant find the multiple definition, when i look on line 1368 in qglobal is says :

inline void qt_noop() {}

So i have no idea where my multiple definition is.

Hope you guys can help me.

Marcel

SixDegrees
9th May 2010, 23:55
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:


#ifndef MY_UNIQUE_SYMBOL_FOR_THIS_FILE
#define MY_UNIQUE_SYMBOL_FOR_THIS_FILE

// Put whatever normally goes in your header file here, as usual

#endif

and see if the problem goes away.