Results 1 to 9 of 9

Thread: Unable to use the operator== of QValueList

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default 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 :
    Qt Code:
    1. QValueList<CClassA> m_vlListOfClassA;
    To copy to clipboard, switch view to plain text mode 

    My problem is in the operator== of CClassB where I have the following code :
    Qt Code:
    1. if( m_vlListOfClassA == ra.m_vlListOfClassA )
    2. return true;
    3. else
    4. return false;
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jacek; 16th June 2006 at 12:31. Reason: changed [ code ] to [ quote ] to allow wrapping of long lines

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to use the operator== of QValueList

    How is the CClassA::operator== defined?

  3. #3
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Unable to use the operator== of QValueList

    As follow
    Qt Code:
    1. bool CClassA::operator==(const CClassA& aca)
    2. {
    3. if( m_dAMember == aca.m_dAMember )
    4. return true;
    5. else
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to use the operator== of QValueList

    Quote Originally Posted by yellowmat
    As follow
    Qt Code:
    1. bool CClassA::operator==(const CClassA& aca)
    2. {
    3. if( m_dAMember == aca.m_dAMember )
    4. return true;
    5. else
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Make it:
    Qt Code:
    1. bool CClassA::operator==(const CClassA& aca) const
    2. {
    3. return ( m_dAMember == aca.m_dAMember );
    4. }
    To copy to clipboard, switch view to plain text mode 
    Note the "const" at the end of function header.

  5. #5
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Unable to use the operator== of QValueList

    I can also give the definition of the operator== of CClassB
    Qt Code:
    1. bool CClassB::operator==(const CClassB& acb)
    2. {
    3. if( m_vlListOfClassA == acb.m_vlListOfClassA) )
    4. return true;
    5. else
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to use the operator== of QValueList

    Quote Originally Posted by yellowmat
    I can also give the definition of the operator== of CClassB
    Qt Code:
    1. bool CClassB::operator==(const CClassB& acb)
    2. {
    3. if( m_vlListOfClassA == acb.m_vlListOfClassA) )
    4. return true;
    5. else
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Same here -- add "const".

  7. #7
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default 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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:
    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.

  9. #9
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.
    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

Similar Threads

  1. Problem using QValueList
    By yellowmat in forum Newbie
    Replies: 2
    Last Post: 7th February 2006, 20:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.