Could anyone explain the problem that I have here. I think I don't have enough understand about class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include "product.h"

class Order : private Product {
private:
	int quantity_; // So luong san pham
	int sell_price_; // Gia ban moi san pham

public:
	void Input();
	void Output();
	long GetSubTotal();
	long GetVAT();
	long GetProfit();

	void static GenerateOrders(Order orders[], int n);
	void static PrintOrders(Order orders[], int n);

	friend long GetMaxVAT(Order orders[], int n);
	friend void SortOrderByProfit(Order orders[], int n);
	friend void PrintOrdersWithMaxTAX(Order orders[], int n);
	friend long GetSumSubTotal(Order orders[], int n);
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void SortOrderByProfit(Order orders[], int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = n - 1; j > i; j--)
		{
			if (orders[j].GetVAT() > orders[j - 1].GetVAT())
			{
				long temp;
				temp = orders[j].GetVAT();
				orders[j].GetVAT() = orders[j - 1].GetVAT();//problem here!
				orders[j - 1].GetVAT() = temp;
			}
		}
	}
}

It said that the expression must be a modifiable value. Not quite understand
Last edited on
So... let's ignore for a second that GetVAT doesn't return a reference (which is why you're getting your error, and should get one on the very next line as well).

You're trying to sort orders by their VATs (in a function called SortOrderByProfit, by the way). To sort an array of objects by some criteria, you'd still compare them using that criteria, but then you'd swap the objects. In your case, you'd still compare their VATs (or Profits), but then you need to swap the order objects themselves, not their VATs/Profits. Makes sense?

-Albatross
here's a simplified version
1
2
3
4
5
int foo(){
   return 42;
}

foo() = 13;
which would be the same than 42 = 13;

what you may do
1
2
3
4
5
Order temp(orders[j]);
orders[j] = orders[j-1];
orders[j-1] = temp;
//equivalent to
std::swap(orders[j], orders[j-1]);


may also use std::sort() with a custom comparator
Last edited on
#ne555 #Albatross

oh I see, I just forgot that GetVAT() just a function not the array that I'm working with. Thanks a lot!
Last edited on
Topic archived. No new replies allowed.