关于此左值的错误,此左值需要作为赋值的左操作数

Error about this lvalue required as left operand of assignment

本文关键字:赋值 操作数 错误 关于此      更新时间:2023-10-16

这是我的问题!!

for( i = 0; i <= MAX - 2; i++){
    for( j = i + 1; j <= MAX - 1; j++){
        if(stud[i].getEdad() < stud[j].getEdad()){
            temp=stud[i].getEdad();
            stud[i].getEdad() = stud[j].getEdad();
            stud[j].getEdad() = temp;
        }
    }
}

我认为问题的根源是

stud[i].getEdad() = stud[j].getEdad().

左值(定位器值)表示在内存中占据某个可识别位置的对象(即具有地址)。右值是通过排除来定义的,即每个表达式要么是左值,要么是右值。因此,从左值的上述定义来看,右值是一个表达式,它不表示对象在内存中占据某个可识别位置

话虽如此,赋值期望左值作为其左操作数,即

//Here foo returns a reference i.e lvalue so ther's no problem in below code.
int& foo()
{
    return globalvar;
}
int main()
{
    foo() = 10;
    return 0;
}
//whereas following one will give you an error since return value is just a temporary object and you are assigning to it.
int foo()
{
    return globalvar;
}
int main()
{
    foo() = 10;
    return 0;
}