Error in bubblesort using class definitions

Hi,

I thought a bubblesort would be the best way to compare the area of different countries in which the values are stored in a private member variable in a class and this is what i came up with but i am getting an error of:

167 : non-lvalue in assignment
168 : non-lvalue in assignment

Can anyone tell me what the issue is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
         double tempArea = 0;
 	 bool     swapped;

	 do {
		 swapped = false;
		 for(int i=0; i<x-1; i++)
			 if(info[i].getArea() < info[i+1].getArea())
			 {
			 	 tempArea = info[i].getArea();
				 info[i].getArea() = info[i+1].getArea();
				 info[i+1].getArea() = tempArea;
				 swapped = true;
			 }
	 }while (swapped);
Last edited on
closed account (D80DSL3A)
The getArea function probably returns a double value? It would have to return a reference to some class member ( area? ) in order for those assignments to work.

Swapping just the areas may be a poor idea anyways. The objects in the info array probably have other members, like the name of the country perhaps, and more?

You probably want to swap the entire object, not just the area values. I can't tell what type is stored in the info array from the given code snippet, so let's say it's a class named Country.

Swap the entire Country objects, with the sorting based on the area values:
1
2
3
4
5
6
7
if(info[i].getArea() < info[i+1].getArea())
 {
	 Country temp = info[i];
	 info[i] = info[i+1];// swap the entire objects, not just some part of them.
	 info[i+1] = temp;
	 swapped = true;
			 }

EDIT: I just checked your other thread. Was that a good guess at the class name, or what?
Last edited on
Yes that was the class name, and i am swapping the name of country and population number along with the area, i get the none-lvalue in assignment error on a couple lines.
Topic archived. No new replies allowed.