Passing objects from a swapfunction

I need to sort the vector and have made a swap function. I can´t pass the name and age to the function and don´t know what I´m doing wrong.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  Put the code you need help with here.// Bubblesort.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>

using namespace std; 

//Klass: contain a person with name and age
class Person
{
public:
	char* name;
	int age;
	//Metod: Sets the necessary info
	void SetInfo(char* _name, int _age)
	{
		name = _name;
		age = _age;
	}
};

// Function Declarations
int LinearSearch(vector<Person>myList, int);
//LinearSearch

void swap(char* name, int age); 
// Swap

void BubbleSort(vector<Person>myList, char*, int);
// Bubblesort 

// Function: Main, start on program
int main()
{
	//Create a list with persons and fill it:
	  
	vector<Person>myList;
	myList[0].SetInfo("Charles", 22);
	myList[1].SetInfo("Darwin", 21);
	myList[2].SetInfo("Phytagoras", 23);
	myList[3].SetInfo("Michelle", 65);
	myList[4].SetInfo("Carl", 76);
	myList[5].SetInfo("Cathrine", 54);
	myList[6].SetInfo("Marielle", 25);
	myList[7].SetInfo("Patrick", 85);
	myList[8].SetInfo("Oliver", 48);
	myList[9].SetInfo("Elinette", 52);

	//Search for a person in the list 
	int index = LinearSearch(myList, 54);

	//Write out searchresults
	if(index == -1)
		cout << "Person wasn´t found!";
	else
		cout << "The person you are looking for is named" << myList[index].name << " and is on index " << index << "\n\n";
	
	BubbleSort(myList); 

	return 0;
}

// Function Definition

// Funktion: LinearSearch: Search for a person in a list
int LinearSearch(vector<Person>myList, int key)
{
	for (int i = 0; i < 10; i++) //Go through the whole list
	{
		// Is it the number we are searching for?
		if (myList[i].age == key)
			return i;
	}
	return -1; // Person were not found
}

// BubbleSort, sorting Person myList[] by age
void BubbleSort(vector<Person>myList)
{
  	int max = 9;
	// The outer loop, going through all list
    for(int i = 0; i < max; i++)
	{

		//Inner loop, going through element by element
		int nrLeft = max - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft; j++)
		{
			
			if (myList[j] > myList[j+1]) // Compare the elements, << give error
			{
				swap(&p, &q); << give error
			}
		}
	}
		// Write the list
	for (int i = 0; i < 9; i++)
	{
		cout << swap(&p, &q) << "\n"; // << give error
	}
}

void swap(Person &p, Person &q) 
{ 
	Person temp; 
	temp.name = p.name; 
	temp.age = p.age; 
	p.name= q.name; 
	p.age = q.age; 
	q.name = temp.name; 
	q.age = temp.age; 

	return;
}


There are too many problems with this code, some more serious than others. I'll try and point out some of them, but I'm sure I'll miss several.

Data members are public, despite the SetInfo() function

name is just a raw pointer, there's no storage associated with it, so when you assign to it, you're asking for trouble. You should have either a static char array (or dynamically allocate memory) and strcpy to it in setinfo.
Better yet, use a std::string

Also, in main you're passing const char* in name_ instead of char *

A bigger problem is your vector. It's most likely empty and you're indexing into it. You should create your Person objects and then use vector::push_back to add them.

Better yet, write a constructor for your class and use that.

Summarising so far:

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
// Bubblesort.cpp : Defines the entry point for the console application.
//

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

using namespace std; 

class Person
{
public:
	Person(const string &_name, int _age) : name(_name), age(_age) {}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _age)
	{
		name = _name;
		age = _age;
	}
	
	const string &Name() const { return name; }
	int Age() const { return age;}
	
private:
	string name;
	int age;
};

// Function: Main, start on program
int main()
{
	//Create a list with persons and fill it:
	  
	vector<Person> myList;
	cout << "size of myList is " << myList.size() << endl;
	myList.push_back(Person("Charles", 22));
	myList.push_back(Person("", 0));
	//...
	
	// later
	myList[1].SetInfo("Darwin", 21);
	//...
	
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
	
	cout << "size of myList is " << myList.size() << endl;
}


The prototype of BubbleSort() is incompatible with how it is defined and used
In BubbleSort, p and q are never defined, hence the "give error"
You've not defined a comparison operator for Person, so the comparison in line 91 is illegal, I take it you mean to comapre their ages
Also, for the swap, it's better to use a copy constructor or assignment operator, I've added both and changed swap below
The bubblesort has an off by one error which will eventually crash the program,
the arbitrary max = 9 is unnecessary:

Example without the Linear search:
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
97
98
99
100
101
102
103
104
105
106
 // Bubblesort.cpp : Defines the entry point for the console application.
//

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

using namespace std; 

class Person
{
public:
	Person() : name(""), age(0) {}
	Person(const string &_name, int _age) : name(_name), age(_age) {}
	Person(const Person &rhs) : name(rhs.name), age(rhs.age) {}
	Person operator=(const Person &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		age = rhs.age;
		return *this;
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _age)
	{
		name = _name;
		age = _age;
	}
	
	const string &Name() const { return name; }
	int Age() const { return age;}
	
private:
	string name;
	int age;
};

void BubbleSort(vector<Person> &myList);
void swap(Person &p, Person &q);

// Function: Main, start on program
int main()
{
	//Create a list with persons and fill it:
	  
	vector<Person> myList;
	cout << "size of myList is " << myList.size() << endl;
	myList.push_back(Person("Charles", 22));
	myList.push_back(Person("", 0));
	myList.push_back(Person("Michelle", 65));
	myList.push_back(Person("Carl", 76));
	myList.push_back(Person("Cathrine", 54));
	myList.push_back(Person("Marielle", 25));
	myList.push_back(Person("Patrick", 85));
	myList.push_back(Person("Oliver", 48));
	myList.push_back(Person("Elinette", 52));
	
	// later
	myList[1].SetInfo("Darwin", 21);
	//...
	
	cout << "\nsize of myList is " << myList.size() << endl;

	cout << "\nBefore sort:" << endl;
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
	
	BubbleSort(myList);
	cout << "\nAfter sort:" << endl;
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
}

void BubbleSort(vector<Person> &myList)
{
	// The outer loop, going through all list
    for(int i = 0; i < myList.size(); i++)
	{
		//Inner loop, going through element by element
		int nrLeft = myList.size() - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (myList[j].Age() > myList[j+1].Age()) // Compare the elements
			{
				swap(myList[j], myList[j+1]); 
			}
		}
	}
}

void swap(Person &p, Person &q) 
{ 
	Person temp = p; 
	p = q;
	q = temp;
}


Thanks tipaye! You solved Everything, now I have to study it. Incredible. Many thanks my friend! :)

Regards / P
Topic archived. No new replies allowed.