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

1
2
3
4
5
6
  void swap (vector<Person*> persPtr1 , vector<Person*> 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:
1
2
3
4
5
6
void swap(Person& p1, Person& p2)
{
    Person temp = p1;
    p1 = p2;
    p2 = temp;
}


How to swap a vector<Person*>:
1
2
3
4
5
6
void swap(vector<Person*>& p1, vector<Person*>& p2)
{
    vector<Person*> temp = p1;
    p1 = p2;
    p2 = temp;
}


How to swap anything that has operator= defined:
1
2
3
4
5
6
7
template <typename T>
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Person.h"

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void selectionSort ( vector<Person*> 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<Person*> 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<Person*> 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.