Results 1 to 5 of 5

Thread: myclass::operator==() overload problem.

  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default myclass::operator==() overload problem.

    Hello

    Qt4.3.1 Open Source Edition for Windows XP (Pro SP2), configured as "-release -shared".

    I would like to overload the "==" operator for the objects of my custom class.
    Let's have a look to my class :

    Qt Code:
    1. #ifndef donnee_h
    2. #define donnee_h
    3.  
    4. #include <QList>
    5. #include <QString>
    6. #include <QStringList>
    7.  
    8. class donnee
    9. {
    10.  
    11. public:
    12. donnee();
    13.  
    14. bool operator==(const donnee&) const;
    15.  
    16. QString nom_donnee; // Nom du répertoire ou du film concerné par la donnée
    17. QString access_donnee; // Chemin d'accès au répertoire ou du film concerné par la donnée
    18. QString type_donnee; // "Film" ou "Répertoire"
    19.  
    20. QStringList *liste_noms_des_films; // Si la donnée est un répertoire
    21. QStringList *liste_acces_des_films; // Si la donnée est un répertoire
    22.  
    23. void trier_listes_des_films();
    24.  
    25. private:
    26.  
    27. };
    28.  
    29. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. #include "donnee.h"
    5.  
    6. donnee::donnee()
    7. {
    8. nom_donnee = "";
    9. access_donnee = "";
    10. type_donnee = "";
    11.  
    12. liste_noms_des_films = new QStringList();
    13. liste_acces_des_films = new QStringList();
    14. }
    15.  
    16. bool donnee::operator==(const donnee &a) const
    17. {
    18. cout << "OPERATOR: " << a.nom_donnee.toStdString() << "\t" << nom_donnee.toStdString() << endl;
    19. if (a.nom_donnee != nom_donnee) return 0;
    20. if (a.access_donnee != access_donnee) return 0;
    21. if (a.type_donnee != type_donnee) return 0;
    22. if (a.liste_noms_des_films != liste_noms_des_films) return 0;
    23. if (a.liste_acces_des_films != liste_acces_des_films) return 0;
    24. return 1;
    25. }
    26.  
    27. void donnee::trier_listes_des_films()
    28. {
    29. QStringList list_temp_films;
    30. QStringList list_temp_access;
    31. QString current_name = "";
    32. QString smallest_name = "";
    33. int indice_smallest_name;
    34.  
    35. // Remplissage de listes temporaires et triées
    36. int NB_tours = liste_noms_des_films->size();
    37. for (int i = 0; i < NB_tours; ++i)
    38. {
    39. smallest_name = liste_noms_des_films->at(0);
    40. indice_smallest_name = 0;
    41.  
    42. for (int j = 0; j < liste_noms_des_films->size(); ++j)
    43. {
    44. current_name = liste_noms_des_films->at(j);
    45.  
    46. if(QString::compare(current_name, smallest_name, Qt::CaseInsensitive) < 0)
    47. {
    48. smallest_name = current_name;
    49. indice_smallest_name = j;
    50. }
    51. }
    52. list_temp_films.append(liste_noms_des_films->takeAt(indice_smallest_name));
    53. list_temp_access.append(liste_acces_des_films->takeAt(indice_smallest_name));
    54. }
    55.  
    56. // Mises à jour des listes, à partir des listes temporaires
    57. liste_noms_des_films->clear();
    58. for (int k = 0; k < list_temp_films.size(); ++k)
    59. {
    60. liste_noms_des_films->append(list_temp_films.at(k));
    61. }
    62.  
    63. liste_acces_des_films->clear();
    64. for (int l = 0; l < list_temp_access.size(); ++l)
    65. {
    66. liste_acces_des_films->append(list_temp_access.at(l));
    67. }
    68.  
    69. }
    To copy to clipboard, switch view to plain text mode 

    This is a very simple class managing QStrings and QLists of QStrings.
    I've reimplemented the "==" operator.

    The class is correctly built, and I get no errors or warnings.
    So I test my new "==" operator with the 2 following examples :

    Qt Code:
    1. donnee * data_1 = new donnee();
    2. data_1->nom_donnee = "nom data 1";
    3. data_1->access_donnee = "access data 1";
    4. data_1->type_donnee = "type data 1";
    5.  
    6. donnee * data_2 = new donnee();
    7. data_2->nom_donnee = "nom data 1";
    8. data_2->access_donnee = "access data 1";
    9. data_2->type_donnee = "type data 1";
    10.  
    11. if (data_1 == data_2) {cout << "yes" << endl;}
    12. else {cout << "no" << endl;}
    To copy to clipboard, switch view to plain text mode 
    This should display "yes", but it displays "no".


    Qt Code:
    1. donnee * data_1 = new donnee();
    2. data_1->nom_donnee = "nom data 1";
    3. data_1->access_donnee = "access data 1";
    4. data_1->type_donnee = "type data 1";
    5.  
    6. donnee * data_2 = new donnee();
    7. data_2->nom_donnee = "nom data 2";
    8. data_2->access_donnee = "access data 2";
    9. data_2->type_donnee = "type data 2";
    10.  
    11. if (data_1 == data_2) {cout << "yes" << endl;}
    12. else {cout << "no" << endl;}
    To copy to clipboard, switch view to plain text mode 
    This should display "no", and it displays "no".


    So my first problem is that my new "==" operator returns always "false".

    Another funny thing is that my new "==" operator should display a message on the cout channel each time it is invocated... But this message is never displayed, like if my new operator wasn't used. And you know what ? If I comment my new operator from donnee.h and donnee.cpp, my application is correctly build and executed, without errors or warnings...

    I think I missed something, could you help me please ?
    This is my first operator overload :s

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: myclass::operator==() overload problem.

    You have defined == operator for donnee class, but you're trying to compare pointers.

    It should be:
    Qt Code:
    1. if( *data_1 == *data_2 ) ...
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    Nyphel (11th October 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: myclass::operator==() overload problem.

    Ooooh yes...
    Thanks Jacek

  5. #4
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: myclass::operator==() overload problem.

    I've adjusted my operator, in order to correct the error.
    Qt Code:
    1. bool donnee::operator==(const donnee &a) const
    2. {
    3. cout << "OPERATOR: " << a.nom_donnee.toStdString() << "\t" << nom_donnee.toStdString() << endl;
    4. if (a.nom_donnee != nom_donnee) return false;
    5. if (a.access_donnee != access_donnee) return false;
    6. if (a.type_donnee != type_donnee) return false;
    7. if (*a.liste_noms_des_films != *liste_noms_des_films) return false;
    8. if (*a.liste_acces_des_films != *liste_acces_des_films) return false;
    9. return true;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Now that the tests are passed, I want to really use this operator .

    I've got a QList (named "liste_donnees") filled with objects of my donnee class.
    I suppose that my QList may contains 2 times the same object, and I would like to clear it in order not to have more than 1 time the same object before displaying its content on my interface.

    So I loop into my Qlist, from start index to end index, and I copy my objects in a temporary QList only if the current object isn't already in this temporary QList. This is the reason why I overloaded my "==" operator. (The "contains()" method for QList requiered me to do it...)

    Qt Code:
    1. QList<donnee *> liste_donnees_temp;
    2.  
    3. for (int i = 0; i < liste_donnees.size(); ++i)
    4. {
    5. if (liste_donnees_temp.contains(liste_donnees.at(i)) == false)
    6. {
    7. liste_donnees_temp.append(liste_donnees.at(i));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    The problem is the same as in my previous post : my operator isn't used.
    The reason seems to be that the same as previously. So I should modify my code like it :

    Qt Code:
    1. QList<donnee *> liste_donnees_temp;
    2.  
    3. for (int i = 0; i < liste_donnees.size(); ++i)
    4. {
    5. if (liste_donnees_temp.contains(*liste_donnees.at(i)) == false)
    6. {
    7. liste_donnees_temp.append(liste_donnees.at(i));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    But when I do a such modification, the compilator returns me an error :
    [no matching function for call 'QList<donnee *>::contains(donnee&)']

    Is there a solution ?
    Must I search in my "liste_donnees_temp" manually, with a loop for example ?

    Thanks for your help !

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: myclass::operator==() overload problem.

    Quote Originally Posted by Nyphel View Post
    if (liste_donnees_temp.contains(*liste_donnees.at(i)) == false)
    Unfortunately, you can't do that. QList<donnee *> is a list of pointers and you can only check whether it contains a given pointer. In Qt 3 there were special collections for storing pointers, but now they're gone.

    It looks like you will have to simply traverse the list and check each item yourself. Something like:
    Qt Code:
    1. template< class T >
    2. bool contains( QList< T * > list, T * item )
    3. {
    4. foreach( T * i, list ) {
    5. if( *i == *item ) {
    6. return true;
    7. }
    8. }
    9. return false;
    10. }
    To copy to clipboard, switch view to plain text mode 
    (untested).

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57

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.