lvalue required as increment operand error

后端 未结 4 2222
故里飘歌
故里飘歌 2020-12-06 12:22
#include 

int main()
{
   int i = 10;
   printf(\"%d\\n\", ++(-i)); // <-- Error Here
}

What is wrong with ++(-i)?

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 12:55

    You can't increment a temporary that doesn't have an identity. You need to store that in something to increment it. You can think of an l-value as something that can appear on the left side of an expression, but in eventually you'll need to think of it in terms of something that has an identity but cannot be moved (C++0x terminology). Meaning that it has an identity, a reference, refers to an object, something you'd like to keep.

    (-i) has NO identity, so there's nothing to refer to it. With nothing to refer to it there's no way to store something in it. You can't refer to (-i), therefore, you can't increment it.

    try i = -i + 1

    #include 
    
    int main()
    {
       int i = 10;
       printf("%d\n", -i + 1); // <-- No Error Here
    }
    

提交回复
热议问题