error C2106: '=' : left operand must be l-value

前端 未结 4 1551
Happy的楠姐
Happy的楠姐 2020-12-20 17:48

Looking at the other questions regarding error C2106, I am still lost as to what the issue is with my code. While compiling I get the following errors:

c

4条回答
  •  误落风尘
    2020-12-20 18:28

    This error is being thrown for the same reason you can't do something like this:

    36 = 3;
    

    Your version of Vector::at should be returning a reference rather than a value.
    Lvalues are called Lvalues because they can appear on the left of an assignment. Rvalues cannot appear on the left side, which is why we call them rvalues. You can't assign 3 to 36 because 36 is not an lvalue, it is an rvalue, a temporary. It doesn't have a memory address. For the same reason, you cannot assign NULL to payroll.at(i).


    Your definition:

    template  V MyVector::at(int n)
    

    What it should be:

    template V& MyVector::at(std::size_t n)
    template const V& MyVector::at(std::size_t n) const
    

提交回复
热议问题