### Problem setting an object equal to a pointer to an object

I am working on a homework problem with pointers and I think I've almost figured out the entire problem but I'm getting one set of errors that I can't figure out. I'm using selection sort to sort a vector of Person* objects by their age. Person has two private variables string name and int age.

The error I'm getting is trying to swap the entire person. I am still somewhat confused on how to explain what I'm doing so this might not make any sense, but the code is below. If anybody has any idea what to do I would really appreciate some help.

Thanks,
Tucker

 ``123456`` `````` void swap (vector persPtr1 , vector persPtr2) { Person temp = persPtr1; persPtr1 = persPtr2; persPtr2 = temp; }``````

EDIT: This is just the swap function, everything else seems to be working
Last edited on
Are you trying to swap a person or a vector<Person*>

How to swap a person:
 ``123456`` ``````void swap(Person& p1, Person& p2) { Person temp = p1; p1 = p2; p2 = temp; }``````

How to swap a vector<Person*>:
 ``123456`` ``````void swap(vector& p1, vector& p2) { vector temp = p1; p1 = p2; p2 = temp; }``````

How to swap anything that has operator= defined:
 ``1234567`` ``````template void swap(T& p1, T& p2) { T temp = p1; p1 = p2; p2 = temp; }``````
I'm trying to swap a Person that is an element of a vector<*Person>. I just tried using what you said up top and it might be working, but when I try to display the vector, element by element, it is printing the address on the screen.

My entire code is below.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include "Person.h" #include #include #include using namespace std; void selectionSort ( vector vec, bool (*compare) (int, int)); void swap ( Person &persPtr1 , Person &persPtr2); bool ascending (int a, int b); bool descending (int a, int b); int main() { string name; int age; vector people; string order; while (name != "q") { cout << "Enter name, q to quit: "; getline(cin, name); if (name != "q") { cout << "Enter age: "; cin >> age; Person* p = new Person(name,age); cin.ignore(); people.push_back(p); } } cout << "Enter the order to sort the people: "; cin >> order; if (order == "ascending") //Ascending { selectionSort(people, ascending); cout << "Ascending order: " << endl; for (int i = 0; i < people.size(); i++) { cout << people.at(i); } } else if (order == "descending") //Ascending { selectionSort(people, descending); cout << "Descending order: " << endl; } system("PAUSE"); return 0; } void selectionSort (vector vec, bool (*compare) (int, int)) { int num; for (int i = 0; i < vec.size(); i++) { num = i; for (int j = i+1; j < vec.size(); j++) { if (!(*compare) (vec.at(num)->getAge(), vec.at(j)->getAge())) { num = j; } } swap (vec.at(num), vec.at(i)); } } void swap (Person& persPtr1 , Person& persPtr2) { Person temp = persPtr1; persPtr1 = persPtr2; persPtr2 = temp; } bool ascending (int a, int b) { return a < b; } bool descending (int a, int b) { return a > b; } ``````

EDIT: I know descending isn't done yet, I was just testing with asceding
Last edited on
Topic archived. No new replies allowed.