Hello,

i try to model the relationshipe between Persons in a Group.

The code looks like this:


dialog.h
Qt Code:
  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5.  
  6. #include "group.h"
  7. #include "person.h"
  8.  
  9.  
  10. namespace Ui {
  11. class Dialog;
  12. }
  13.  
  14. class Dialog : public QDialog
  15. {
  16. Q_OBJECT
  17.  
  18. public:
  19. explicit Dialog(QWidget *parent = 0);
  20. ~Dialog();
  21.  
  22. void test1();
  23.  
  24. private:
  25. Ui::Dialog *ui;
  26. };
  27.  
  28. #endif // DIALOG_H
To copy to clipboard, switch view to plain text mode 

dialog.cpp

Qt Code:
  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3.  
  4. Dialog::Dialog(QWidget *parent) :
  5. QDialog(parent),
  6. ui(new Ui::Dialog)
  7. {
  8. ui->setupUi(this);
  9.  
  10. test1();
  11.  
  12.  
  13. }
  14.  
  15. Dialog::~Dialog()
  16. {
  17. delete ui;
  18. }
  19.  
  20. void Dialog::test1()
  21. {
  22. //create a group
  23. Group group;
  24.  
  25. //create two persons
  26. Person pete = group.addPersonToGroup("Pete");
  27. Person mary = group.addPersonToGroup("Mary");
  28.  
  29. // create relationship
  30. group.knows(pete,"friends",mary);
  31.  
  32. //if the debugger run to this point,
  33. // the member variable mFriends. is empty
  34. // i have expected:
  35. // pete.mFriends contains mary
  36. // mary.mFriends contains pete
  37. int i = 0;
  38. }
To copy to clipboard, switch view to plain text mode 


group.h
Qt Code:
  1. #ifndef GROUP_H
  2. #define GROUP_H
  3.  
  4. #include <QString>
  5.  
  6. #include "person.h"
  7.  
  8. class Group
  9. {
  10. public:
  11. Group();
  12. Person addPersonToGroup(QString name);
  13. void knows(Person p1, QString label, Person p2);
  14. };
  15.  
  16. #endif // GROUP_H
To copy to clipboard, switch view to plain text mode 


group.cpp
Qt Code:
  1. #include "group.h"
  2.  
  3. Group::Group()
  4. {
  5. }
  6.  
  7. Person Group::addPersonToGroup(QString name)
  8. {
  9. Person newPerson;
  10. newPerson.setName(name);
  11.  
  12. return newPerson;
  13. }
  14.  
  15. void Group::knows(Person p1, QString label, Person p2)
  16. {
  17.  
  18. p1.knows(label, p2);
  19. }
To copy to clipboard, switch view to plain text mode 

person.h
Qt Code:
  1. #ifndef PERSON_H
  2. #define PERSON_H
  3.  
  4. #include <QString>
  5.  
  6. #include "personset.h"
  7.  
  8.  
  9. class PersonSet;
  10.  
  11. class Person
  12. {
  13. public:
  14. Person();
  15.  
  16. void setName(QString name);
  17. void knows(QString label, Person p1);
  18.  
  19. private:
  20. QString mName ;
  21. QHash<QString,PersonSet> mFriends;
  22. };
  23.  
  24. #endif // PERSON_H
To copy to clipboard, switch view to plain text mode 


person.cpp
Qt Code:
  1. #include "person.h"
  2.  
  3. Person::Person()
  4. {
  5. }
  6.  
  7. void Person::setName(QString name)
  8. {
  9. mName = name;
  10. }
  11.  
  12. void Person::knows(QString label,Person p1)
  13. {
  14. PersonSet personSet = mFriends[label];
  15.  
  16. if(personSet.empty())
  17. {
  18. // here is the insert of person into mFriends/ QHash
  19. personSet.insert(0,p1);
  20. mFriends.insert(label,personSet);
  21. }
  22.  
  23. personSet.insert(0,p1);
  24.  
  25. if(!label.startsWith("_back_"))
  26. {
  27. p1.knows("_back_"+label,*this);
  28. }
  29. }
To copy to clipboard, switch view to plain text mode 
int
personSet.h
Qt Code:
  1. #ifndef PERSONSET_H
  2. #define PERSONSET_H
  3.  
  4. #include <QHash>
  5.  
  6. #include "person.h"
  7.  
  8. class Person;
  9.  
  10. class PersonSet : public QHash<int,Person>
  11. {
  12. public:
  13. PersonSet();
  14. };
  15.  
  16. #endif // PERSONSET_H
To copy to clipboard, switch view to plain text mode 

personSet.cpp
Qt Code:
  1. #include "personset.h"
  2.  
  3. PersonSet::PersonSet()
  4. {
  5. }
To copy to clipboard, switch view to plain text mode 

Like descripted in the conments of the function Dialog::test1(), the mFriends variable of the typ personSet container class which inherits from QHash<int,Person> is empty after the function Dialog::test1() passes through.

But i do get it why ?

Thx