PDA

View Full Version : QList problem



acix
23rd April 2006, 14:10
Hi everyone,
I have created a list QList <AFile> list;
Now, I know that in order to use the removeAll() method of the list, the operator==()
must be implemented in the class AFile. I did implement it like this:

bool AFile::operator==(AFile &file1) const
{
if(filePath!=file1.getFilePath()) //filepath is a QString
return false;
else
return true;
}
is there anything wrong with it?
when i use the removeAll() the compiler shows the follow errors:


usr/local/Trolltech/Qt-4.1.1/include/QtCore/qlist.h: In member function ‘int QList<T>::removeAll(const T&) [with T = AFile]’:
video/AFileList.cpp:75: instantiated from here
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qlist.h:549: error: no match for ‘operator==’ in ‘(n <unknown operator> ((QList<AFile>::Node*)((QList<AFile>*)this)->QList<AFile>::<anonymous>.QList<AFile>::<anonymous union>::p. QListData::at(i)))->QList<T>::Node::t [with T = AFile]() == t’
video/AFile.h:22: note: candidates are: bool AFile::operator==(AFile&) const
video/AFile.h:23: note: bool AFile::operator==(AFile*) const
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qglobal.h:1266: note: bool operator==(QBool, bool)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qglobal.h:1267: note: bool operator==(bool, QBool)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qglobal.h:1268: note: bool operator==(QBool, QBool)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qchar.h:283: note: bool operator==(QChar, QChar)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qbytearray.h:421: note: bool operator==(const QByteArray&, const QByteArray&)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qbytearray.h:423: note: bool operator==(const QByteArray&, const char*)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qbytearray.h:425: note: bool operator==(const char*, const QByteArray&)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qstring.h:713: note: bool operator==(QString::Null, QString::Null)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qstring.h:714: note: bool operator==(QString::Null, const QString&)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qstring.h:715: note: bool operator==(const QString&, QString::Null)
/usr/local/Trolltech/Qt-4.1.1/include/QtCore/qstring.h:733: note: bool operator==(const char*, const QString&)
gmake[1]: *** [AFileList.o] Error 1
gmake: *** [sub-src-make_default] Error 2
*** Exited with status: 2 ***

thanks in advance!

jacek
23rd April 2006, 14:25
Try:
bool AFile::operator==( const AFile& file1 ) const
{
return ( filePath == file1.getFilePath() );
}
and make sure that getFilePath() is marked as const.

acix
23rd April 2006, 18:35
and make sure that getFilePath() is marked as const.

I did that too , but the same compiler error, I really dont know whats going wrong.

jacek
23rd April 2006, 19:12
Have you declared that operator as public?

acix
26th April 2006, 13:47
Thanks jacek, I found the problem, it was really a stupid detail, messed up with the placement of const keyword, I didnt know well that there was a difference between:

const void function();
and

void function() const;
anyway thanks again for guiding me!

wysota
27th April 2006, 23:57
I didnt know well that there was a difference between:

const void function();
This marks the value returned as non-mutable (or "unmodifiable", "constant" -- you can't change its value).



void function() const;
anyway thanks again for guiding me!
This marks that the method doesn't modify the calling object ("this").

acix
29th April 2006, 14:08
Thanks wysota for clarifying, I do get the difference now :)