“l-value required” error

前端 未结 11 2152
离开以前
离开以前 2020-12-10 00:06

When do we get \"l-value required\" error...while compiling C++ program???(i am using VC++ )

11条回答
  •  攒了一身酷
    2020-12-10 00:56

    An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required.

    For example, a constant is an rvalue but not an lvalue. So:

    1 = 2;  // Not well formed, assigning to an rvalue
    int i; (i + 1) = 2;  // Not well formed, assigning to an rvalue.
    

    doesn't work, but:

    int i;
    i = 2;
    

    Does. Note that you can return an lvalue from a function; for example, you can return a reference to an object that provides a operator=().

    As pointed out by Pavel Minaev in comments, this is not a formal definition of lvalues and rvalues in the language, but attempts to give a description to someone confused about an error about using an rvalue where an lvalue is required. C++ is a language with many details; if you want to get formal you should consult a formal reference.

提交回复
热议问题