fork download
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. class person
  7. {
  8. private:
  9. string name;
  10. string *spouse;
  11.  
  12. public:
  13. void setName(string tempName) { name = tempName; }
  14. void setSpouse(string &tempSpouse) { spouse = &tempSpouse; } // ERROR HERE?
  15.  
  16. string getName() { return name; }
  17. string getSpouse() { return spouse; } // ERROR HERE?
  18. };
  19.  
  20. int main()
  21. {
  22. person entry;
  23. list<person> personList;
  24. list<person>::iterator itr1, itr2;
  25.  
  26. /* Adding two people/nodes to the linked list. */
  27.  
  28. entry.setName("John Doe");
  29.  
  30. personList.push_back(entry);
  31.  
  32. entry.setName("Tina Doe");
  33.  
  34. personList.push_back(entry);
  35.  
  36. /* Attempting to assign Tina Doe as John Doe's spouse. */
  37.  
  38. for (itr1 = personList.begin(); itr1 != personList.end(); itr1++)
  39. {
  40. if (itr1->getName() == "John Doe")
  41. {
  42. for (itr2 = personList.begin(); itr2 != personList.end(); itr2++)
  43. {
  44. if (itr2->getName() == "Tina Doe")
  45. {
  46. itr1->setSpouse(itr2->getName()); // ERROR HERE?
  47. }
  48. }
  49. }
  50. }
  51.  
  52. /* Displaying all Names with Spouses afterwards. */
  53.  
  54. for (itr1 = personList.begin(); itr1 != personList.end(); itr1++)
  55. {
  56. cout << "Name: " << itr1->getName() << " | Spouse: " << itr1->getSpouse() << endl;
  57. }
  58.  
  59. return 0;
  60. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function 'std::string person::getSpouse()':
prog.cpp:17:37: error: could not convert '((person*)this)->person::spouse' from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'
         string getSpouse() { return spouse; } // ERROR HERE?
                                     ^
prog.cpp: In function 'int main()':
prog.cpp:46:52: error: no matching function for call to 'person::setSpouse(std::string)'
                     itr1->setSpouse(itr2->getName()); // ERROR HERE?
                                                    ^
prog.cpp:46:52: note: candidate is:
prog.cpp:14:14: note: void person::setSpouse(std::string&)
         void setSpouse(string &tempSpouse) { spouse = &tempSpouse; } // ERROR HERE?
              ^
prog.cpp:14:14: note:   no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'std::string& {aka std::basic_string<char>&}'
stdout
Standard output is empty