Results 1 to 9 of 9

Thread: QList Overloading operator==()

  1. #1
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QList Overloading operator==()

    Hi!
    I've a list of a custom items and I have implemented function == in my code but it doesn't works!

    This is my code:

    Qt Code:
    1. #ifndef RUTA_H
    2. #define RUTA_H
    3.  
    4. #include <QtCore>
    5.  
    6. class Ruta
    7. {
    8. public:
    9. Ruta();
    10. void setNum(int r);
    11. int getNum();
    12. bool operator ==(const Ruta & r) const;
    13.  
    14. private:
    15. int num;
    16. };
    17. #endif // RUTA_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "ruta.h"
    2.  
    3. Ruta::Ruta()
    4. {
    5. }
    6.  
    7. void Ruta::setNum(int r)
    8. {
    9. num = r;
    10. }
    11.  
    12. int Ruta::getNum()
    13. {
    14. return num;
    15. }
    16.  
    17. bool Ruta::operator ==(const Ruta & r) const
    18. {
    19. return(r.num == this->num);
    20. }
    To copy to clipboard, switch view to plain text mode 

    The main function is:
    Qt Code:
    1. Ruta *a = new Ruta();
    2. Ruta *b = new Ruta();
    3.  
    4. a->setNum(1);
    5. b->setNum(1);
    6.  
    7. if(a == b)
    8. qDebug("ok!");
    9. else
    10. qDebug("no");
    To copy to clipboard, switch view to plain text mode 

    And the output is:
    Qt Code:
    1. no
    To copy to clipboard, switch view to plain text mode 

    I don't know what can I do, because I need It to use a "indexOf" function in a QList<Ruta*>

  2. #2
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList Overloading operator==()

    I've tried this, but doesn't works...

    Qt Code:
    1. bool operator==( Ruta *r1)
    2. {
    3. return r1->getNum() == this->getNum();
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QList Overloading operator==()

    Quote Originally Posted by josepvr View Post
    Hi!
    I've a list of a custom items and I have implemented function == in my code but it doesn't works!

    This is my code:

    Qt Code:
    1. #ifndef RUTA_H
    2. #define RUTA_H
    3.  
    4. #include <QtCore>
    5.  
    6. class Ruta
    7. {
    8. public:
    9. Ruta();
    10. void setNum(int r);
    11. int getNum();
    12. bool operator ==(const Ruta & r) const;
    13.  
    14. private:
    15. int num;
    16. };
    17. #endif // RUTA_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "ruta.h"
    2.  
    3. Ruta::Ruta()
    4. {
    5. }
    6.  
    7. void Ruta::setNum(int r)
    8. {
    9. num = r;
    10. }
    11.  
    12. int Ruta::getNum()
    13. {
    14. return num;
    15. }
    16.  
    17. bool Ruta::operator ==(const Ruta & r) const
    18. {
    19. return(r.num == this->num);
    20. }
    To copy to clipboard, switch view to plain text mode 

    The main function is:
    Qt Code:
    1. Ruta *a = new Ruta();
    2. Ruta *b = new Ruta();
    3.  
    4. a->setNum(1);
    5. b->setNum(1);
    6.  
    7. if(a == b)
    8. qDebug("ok!");
    9. else
    10. qDebug("no");
    To copy to clipboard, switch view to plain text mode 

    And the output is:
    Qt Code:
    1. no
    To copy to clipboard, switch view to plain text mode 

    I don't know what can I do, because I need It to use a "indexOf" function in a QList<Ruta*>
    You are comparing pointers to items, not items themselves. Either make a QList<Ruta> and not QList<Ruta*> or provide a custom operator working on pointers or compare items and not pointers:
    Qt Code:
    1. (*a)==(*b)
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to wysota for this useful post:

    josepvr (27th January 2009)

  5. #4
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList Overloading operator==()

    I need a pointer list, QList<Ruta *>.

    how should be this function to work with a pointer without doing (*a)==(*b)?
    Qt Code:
    1. bool Ruta::operator ==(const Ruta & r) const
    2. {
    3. return(r.num == this->num);
    4. }
    To copy to clipboard, switch view to plain text mode 


    Thanks!

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

    Default Re: QList Overloading operator==()

    Qt Code:
    1. bool operator==(Ruta *one, Ruta *two){
    2. return one->num==two->num;
    3. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to wysota for this useful post:

    josepvr (27th January 2009)

  8. #6
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList Overloading operator==()

    I've a compilation error.

    I've tried this:
    Qt Code:
    1. #ifndef RUTA_H
    2. #define RUTA_H
    3.  
    4. #include <QtCore>
    5.  
    6. class Ruta
    7. {
    8. public:
    9. Ruta();
    10. void setNum(int r);
    11. int getNum();
    12.  
    13. bool operator==(Ruta *one, Ruta *two)
    14. {
    15. return one->num==two->num;
    16. }
    17.  
    18. private:
    19. int num;
    20. };
    21. #endif // RUTA_H
    To copy to clipboard, switch view to plain text mode 
    I got this error:
    Qt Code:
    1. D:/ruta.h:38: error: `bool Ruta::operator==(Ruta*, Ruta*)' must take exactly one argument
    To copy to clipboard, switch view to plain text mode 

    And then I've tried this:

    Qt Code:
    1. #ifndef RUTA_H
    2. #define RUTA_H
    3.  
    4. #include <QtCore>
    5.  
    6. class Ruta
    7. {
    8. public:
    9. Ruta();
    10. void setNum(int r);
    11. int getNum();
    12.  
    13. private:
    14. int num;
    15. };
    16.  
    17. bool operator==(Ruta *one, Ruta *two)
    18. {
    19. return one->getNum() == two->getNum();
    20. }
    21.  
    22. #endif // RUTA_H
    To copy to clipboard, switch view to plain text mode 

    i've another error:
    Qt Code:
    1. D:/ruta.h:43: error: `bool operator==(Ruta*, Ruta*)' must have an argument of class or enumerated type
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  9. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList Overloading operator==()

    Unfortunately, if both operator==() parameters are pointers, it will be a pure comparison of pointer addresses and will not use the user-defined conversion:

    C++ FAQ Lite: [26.10] Can I define an operator overload that works with built-in / intrinsic / primitive types?
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    josepvr (28th January 2009)

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

    Default Re: QList Overloading operator==()

    Hmm... right... in that case the easiest way is to keep the asterisks or get rid of pointers

  12. The following user says thank you to wysota for this useful post:

    josepvr (28th January 2009)

  13. #9
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList Overloading operator==()

    Thanks!
    I've solved creating a function like this:
    Qt Code:
    1. bool equals(Ruta *r1,Ruta *r2);
    To copy to clipboard, switch view to plain text mode 
    and another custom function to shearch this object into a list.

    Thanks everybody!

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 19:55
  2. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 01:06
  3. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 21:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.