Reputation: 59
void Sort(Maximumas Maxi[], int ind){
int m = 0; // begin
bool bk = true;
while(bk){
bk = false;
for(int j=ind-1; j>m; j--){
if(Maxi[j].GetQuantity() > Maxi[j-1].GetQuantity()){
bk = true;
int c = Maxi[j].GetQuantity();
Maxi[j].GetQuantity() = Maxi[j-1].GetQuantity();
Maxi[j-1].GetQuantity() = c;
}
}
m++;
}
}
When I try to compile this code I get an error: expression must be a modifiable lvalue
What's wrong with my code?
Upvotes: 0
Views: 819
Reputation: 227390
My guess would be that Maximumas::GetQuantity()
returns by value or const
reference, so you cannot do this kind of thing:
Maxi[j-1].GetQuantity() = c;
You need an overload that returns a reference. Assuming it returns int
, you need something like
int& GetQuantity();
const int& GetQuantity() const;
where the const
overload allows you to call the member on const
instances or via const
references.
Note that unless this is an exercise, it would be simpler to use std::sort
with a suitable predicate.
Upvotes: 3
Reputation: 5118
I would need to have the signature of Maximumas::GetQuantity()
to be sure, but it looks like this method returns a non-modifiable type, so that you can't assign a value to it, e.g. int
or const int&
.
This would explain why the two following lines fail:
Maxi[j].GetQuantity() = Maxi[j-1].GetQuantity();
Maxi[j-1].GetQuantity() = c;
You should change your method to return a modifiable type, like int&
, to make it work.
Upvotes: 1