Issue with sorting a vector of class objects using bubble sort

I am trying to create a vector of class objects, sort the vector using bubble sort and then eventually print the sorted vector in a table format with the area of the circle displayed next to the correct radius. I am having trouble with the bubble sort function. I am trying to call the function on vector circles but I get an error.
[Error] invalid initialization of reference of type 'std::vector<int>&' from expression of type 'std::vector<Circle>'
I am not sure how to make this work. Any help would be appreciated.

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
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

class Circle {
	
	private:
	
		double radius;				// Radius of circle
		
	public:
		
		// Default constructor
		Circle() {
			radius = 0.0;
		}

		// Set the radius of the circle
		void setRadius(double r) {
			radius = r;
		}
		
		// Get the radius of the circle
		double getRadius() {
			return radius;
		}
		
		// Calculate the area of the circle using current radius
		double area() {
			return(3.1416 * radius * radius);
		}
};
void printVector(vector<Circle> circles);
void bubbleSort(vector<int>& circles);


int main(int argc, char** argv) {
	cout << "Sorted Vector of Circles" << endl;
	cout << "------------------------" << endl;
	
	vector<Circle> circles;

	Circle c1;
	c1.setRadius(2.5);
	circles.push_back(c1);
	
	Circle c2;
	c1.setRadius(3.5);
	circles.push_back(c2);
	
	Circle c3;
	c1.setRadius(1.0);
	circles.push_back(c3);
	
	Circle c4;
	c1.setRadius(5.5);
	circles.push_back(c4);
	
	Circle c5;
	c1.setRadius(4.8);
	circles.push_back(c5);
	
	Circle c6;
	c1.setRadius(6.0);
	circles.push_back(c6);
	
	Circle c7;
	c1.setRadius(2.75);
	circles.push_back(c7);
	
	Circle c8;
	c1.setRadius(10);
	circles.push_back(c8);
	
	Circle c9;
	c1.setRadius(0.5);
	circles.push_back(c9);
	
	Circle c10;
	c1.setRadius(9.5);
	circles.push_back(c10);
	
	bubbleSort(circles);

	return 0;
}

void bubbleSort(vector<int>& circles){
      bool swapp = true;
      while(swapp){
        swapp = false;
        for (size_t i = 0; i < circles.size()-1; i++) {
            if (circles[i]>circles[i+1] ){
                circles[i] += circles[i+1];
                circles[i+1] = circles[i] - circles[i+1];
                circles[i] -=circles[i+1];
                swapp = true;
            }
        }
    }
}

void printVector(vector<Circle> circles){
	
}
hi,


bubbleSort is expecting int but you send it circles :+)

There are also problems with using the operators > and += with circle type. You need to tell the compiler how to do those things. What were you thinking there? You want to add / subtract circles ? Consider retrieving the appropriate value and then do comparisons.
Last edited on
Hey,
Thank you for responding.
I am trying to compare the radius value that I sent in for each of the circles.
So I have a vector of circle objects and to compare them I need to convert them to a vector of ints? Or retrieve the int value to compare? I am so lost. I know there has to be a better way to do this and I am just not finding it.
Last edited on
Retrieve the radius using the get radius function and compare them. You also need to think clearly about what it means to swap things.
Topic archived. No new replies allowed.