冒泡排序中赋值 c++ 的左操作数需要左值

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

我看到这个问题有很多不同的答案,并且已经查看了其中的许多答案,但我找不到我的问题的答案。 我有这个错误

lvalue required as left operand of assignment

我正在使用冒泡排序函数对我的对象数组中的双精度值进行排序

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;
            }
        }
    }
}

在我的class

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

为了使像 student[j+1].getBal()=temp; 这样的语句有意义,getBal() 必须 return 引用 到 class。然后,您的语句将修改该成员变量的值 through 该引用。

但不要那样做:更正常的做法是提供一个 setBal() 方法,该方法将 double 作为参数。

学生[j+1].getBal()=temp;

函数调用是 "Rvalue"。这意味着您不能分配它们。 您正在做的事情有点像尝试编写 1 = temp