c++报错问题解决方案lvalue required as left operand of assignment

在编程时出现报错:

lvalue required as left operand of assignment

出现此错误原因,是因为,等号左边是不可被修改的表达式或常量。而表达式或常量不能作为左值。归根结底类似于

3=b;

这种错误。

而查看代码发现,是判断出了问题

if(!strA.compare(strB)&&!strC.compare(strD)&&n1=n2){
    ...
}

由于n1==n2漏写一个等于号,造成括号内由判断条件变成了赋值语句:左值=n2。而由!strA.compare(strB)&&!strC.compare(strD)&&n1组成的是一个布尔值常量。显然是不能作为左值的。

原文地址:https://blog.csdn.net/m0_60352504/article/details/123586741