As follow
Qt Code:
bool CClassA::operator==(const CClassA& aca) { if( m_dAMember == aca.m_dAMember ) return true; else return false; }To copy to clipboard, switch view to plain text mode
As follow
Qt Code:
bool CClassA::operator==(const CClassA& aca) { if( m_dAMember == aca.m_dAMember ) return true; else return false; }To copy to clipboard, switch view to plain text mode
Make it:Originally Posted by yellowmat
Note the "const" at the end of function header.Qt Code:
bool CClassA::operator==(const CClassA& aca) const { return ( m_dAMember == aca.m_dAMember ); }To copy to clipboard, switch view to plain text mode
I can also give the definition of the operator== of CClassB
Qt Code:
bool CClassB::operator==(const CClassB& acb) { if( m_vlListOfClassA == acb.m_vlListOfClassA) ) return true; else return false; }To copy to clipboard, switch view to plain text mode
Same here -- add "const".Originally Posted by yellowmat
Thanks a lot Wysota, it compiles.
I have another question about my problem, why must I add the const in the definition ? What does it means ?
It is the first time I must do this with my operator== ... but it is also the first time I use it with a QValueList.
It means that by comparing the "current" (this) object to some other object you don't modify this object.
And this results in making the "current" object "const", which was missing here:
It's strictly a C++ issue and has nothing to do with Qt.no operator defined which takes a left-hand operand of type 'const class CClassA'
const is pretty handy, since it will catch some programmer errors. Like, when you accidentally try to change a value you aren't supposed to.
The general rule is: Either don't use const at all, or use it everywhere.
QT wisely does use it, so you have to use it as well if you use QT classes.
The compiler was only warning you. You were basically calling a non-const function from a const function . Or rather, the QValueList was doing this, since the CClassA operator== was not const and the QValueList operator== is.
Last edited by Michiel; 18th June 2006 at 17:14.
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
Bookmarks