Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

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

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:driver.cpp(99): error C2106: '=' : left operand must be l-value

c:driver.cpp(169): error C2106: '=' : left operand must be l-value

The line of code is as follows:

payroll.at(i) = NULL; //Line 99
payroll.at(count++) = ePtr; //Line 169

I am failing to understand why this error is being thrown. In this project I have changed my driver.cpp from an array of employee object pointers to a custom Vector template that I made. I declare the Vector as follows...

//Declare an Vector to hold employee object pointers
MyVector <employee*> payroll;

Any help is appreciated...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 <class V> V MyVector<V>::at(int n)

What it should be:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...