使用C ++时,需要左值作为赋值错误的左操作数 [英] lvalue required as left operand of assignment error when using C++

查看:433
本文介绍了使用C ++时,需要左值作为赋值错误的左操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{

   int x[3]={4,5,6};
   int *p=x;
   p +1=p;/*compiler shows error saying 
            lvalue required as left 
             operand of assignment*/
   cout<<p 1;
   getch();
}

推荐答案

在语句中有赋值运算符时,该运算符的LHS必须是该语言称为 lvalue 的东西.如果操作员的LHS不能评估为 lvalue ,则不能将RHS中的值分配给LHS.

When you have an assignment operator in a statement, the LHS of the operator must be something the language calls an lvalue. If the LHS of the operator does not evaluate to an lvalue, the value from the RHS cannot be assigned to the LHS.

您不能使用:

10 = 20;

因为10的求值结果不是左值.

您可以使用:

int i;
i = 20;

因为i的计算结果为左值.

您不能使用:

int i;
i + 1 = 20;

因为i + 1的求值结果不是左值.

在您的情况下,p + 1不会计算为 lavalue .因此,您不能使用

In your case, p + 1 does not evaluate to an lavalue. Hence, you cannot use

p + 1 = p;

这篇关于使用C ++时,需要左值作为赋值错误的左操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆