for eg i have my own vector class and i have created an object v of size 5,now i want to assign values to this object as
v[0]=something,v[1]=something and so on…,
but it is giving error as lvalue required ,
have a look on my code and tell how to overload assignment operator
Overload assignment operator
I`ll debug the solution and get back to u in a while
I have figured out a way to add value to the last position of the array, using overloading of = operator
The following code snippet gives an idea .
void operator = (const T value ) {
a[cs++] = value;
}
v = 2;
Since = operator can take just 1 argument the index cannot be specified.
So the value is appended to the end of the array
see basically why u r getting the error is that
u have overloaded the [] operator
which returns u the value at the specified address
and then
we are willing to overload the = operator giving it a functionality of assiging the value at specified index
but using
v[2] = 3
the complier understands it in the following way
v[2] would give u the value at index 2 that would be a literal constant of type T , then we are trying to assign a another value to the constant we have obatined
lets suppose value at v[2] = 4
, so what we are trying to do is
4 = 3
which is why we get lvalue much be modifiable
You need to comment out either of the overloading scenarios
Refer to this code by prateek bhaiya
it has all the operations performed
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.