PDA

View Full Version : Unable to use the operator== of QValueList



yellowmat
16th June 2006, 10:48
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 :

QValueList<CClassA> m_vlListOfClassA;

My problem is in the operator== of CClassB where I have the following 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 :

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.

wysota
16th June 2006, 10:54
How is the CClassA::operator== defined?

yellowmat
16th June 2006, 10:56
As follow


bool CClassA::operator==(const CClassA& aca)
{
if( m_dAMember == aca.m_dAMember )
return true;
else
return false;
}

wysota
16th June 2006, 11:05
As follow


bool CClassA::operator==(const CClassA& aca)
{
if( m_dAMember == aca.m_dAMember )
return true;
else
return false;
}

Make it:

bool CClassA::operator==(const CClassA& aca) const
{
return ( m_dAMember == aca.m_dAMember );
}
Note the "const" at the end of function header.

yellowmat
16th June 2006, 11:06
I can also give the definition of the operator== of CClassB


bool CClassB::operator==(const CClassB& acb)
{
if( m_vlListOfClassA == acb.m_vlListOfClassA) )
return true;
else
return false;
}

wysota
16th June 2006, 11:07
I can also give the definition of the operator== of CClassB


bool CClassB::operator==(const CClassB& acb)
{
if( m_vlListOfClassA == acb.m_vlListOfClassA) )
return true;
else
return false;
}


Same here -- add "const".

yellowmat
16th June 2006, 11:12
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.

wysota
16th June 2006, 11:48
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:

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.

Michiel
18th June 2006, 18:09
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.