Unable to use the operator== of QValueList
Hi !
I have a compilation error whilie trying to use the operator == of a QValueList and I don't know why.
Here is the problem :
I have a simple class named CClassA which has :
* a default constructor
* a copy constructor
* an operator =
* an operator ==
I did compile and test the code of this class and everything is good.
I have another class named CClassB which has the same kind of constructors and operators, but it also has a member declared as follow :
Code:
QValueList<CClassA> m_vlListOfClassA;
My problem is in the operator== of CClassB where I have the following code :
Code:
if( m_vlListOfClassA == ra.m_vlListOfClassA )
return true;
else
return false;
It seems good to me but the compilation always fails and send the following message :
Quote:
c:\qt\3.3.3\include\qvaluelist.h(582) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'const class CClassA' (or there is no acceptable conversion)
c:\qt\3.3.3\include\qmap.h(240) : while compiling class-template member function 'bool __thiscall QValueList<class CClassA>::operator ==(const class QValueList<class CClassA> &) const'
What should I do in my operator== to check if both list content are equal if it is not possible to use the QValueList operator== ? Should I check every objet of boths lists ?
Thanks in advance.
Re: Unable to use the operator== of QValueList
How is the CClassA::operator== defined?
Re: Unable to use the operator== of QValueList
As follow
Code:
bool CClassA::operator==(const CClassA& aca)
{
if( m_dAMember == aca.m_dAMember )
return true;
else
return false;
}
Re: Unable to use the operator== of QValueList
Quote:
Originally Posted by yellowmat
As follow
Code:
bool CClassA::operator==(const CClassA& aca)
{
if( m_dAMember == aca.m_dAMember )
return true;
else
return false;
}
Make it:
Code:
bool CClassA::operator==(const CClassA& aca) const
{
return ( m_dAMember == aca.m_dAMember );
}
Note the "const" at the end of function header.
Re: Unable to use the operator== of QValueList
I can also give the definition of the operator== of CClassB
Code:
bool CClassB::operator==(const CClassB& acb)
{
if( m_vlListOfClassA == acb.m_vlListOfClassA) )
return true;
else
return false;
}
Re: Unable to use the operator== of QValueList
Quote:
Originally Posted by yellowmat
I can also give the definition of the operator== of CClassB
Code:
bool CClassB::operator==(const CClassB& acb)
{
if( m_vlListOfClassA == acb.m_vlListOfClassA) )
return true;
else
return false;
}
Same here -- add "const".
Re: Unable to use the operator== of QValueList
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.
Re: Unable to use the operator== of 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:
Quote:
no operator defined which takes a left-hand operand of type 'const class CClassA'
It's strictly a C++ issue and has nothing to do with Qt.
Re: Unable to use the operator== of QValueList
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.