Search code examples
c++sortingbubble-sortlvalue

lvalue required as left operand of assignment c++ in Bubble Sort


I see many different answers to this question and have looked at many of them yet I cannot find the answer to my problem. I have this error

lvalue required as left operand of assignment

I'm using bubble sort function to sort double value in my array of objects

void BubbleSort(Student* student=new Student[5])
{
    double temp;
    for(int i2=0; i2<=4; i2++) {
        for(int j=0; j<4; j++) {
            if(student[j].getBal() > student[j+1].getBal()) {
                temp = student[j].getBal();
                student[j] = student[j+1];
                student[j+1].getBal() = temp;
            }
        }
    }
}

In my class

double getBal()
{
    return this->bal;
}
void setBal(double bal)
{
    this->bal=bal;
}

Solution

  • For a statement like student[j+1].getBal()=temp; to make sense, getBal() would have to return a reference to a member variable of the class. Your statement would then modify the value of that member variable through that reference.

    But don't do it that way: the more normal thing to do would be to provide a setBal() method that takes a double as a parameter.