lvalue required as left operand of assignment error

hello!
im trying to change a fields in my class through a function and im getting an error.
here is a simple example of what im trying to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
class A{
	int* a;
public:
	int* returnA();
	void change();
};

int* A::returnA(){
	return this->a;
}
void A::change(){
	int s=10;
	returnA()=&s;// lvalue required as left operand of assignment error
	return;
}



why im getting this error? and what can i do to solve it?

thanks alot!
The sub-expression returnA() doesn't actually return `a' itself, but rather the value of `a'. Unfortunately, values are not variables -- and while you can assign things to variables, you cannot do the same to a value.

In C++-speak, the result of that expression is temporary (specifically it is a prvalue), which occupies no storage (it has no address) and will cease to exist at the semicolon. In other words, assigning to it doesn't make any sense.

If you want to assign a a value through the result, you can return a lvalue reference to a. The reference refers to the variable a itself; all operations on references are treated as if applied to the referent.

1
2
3
int*& A::returnA(){ // return a lvalue reference to this->a
	return this->a;
}

Another problem with your code is here:
1
2
3
4
5
void A::change(){
	int s=10;
	returnA()=&s;// uh oh - assigns an address of a local variable
	return;
} // the lifetime of s ends exactly here 
the local variable s ceases to exist when the function returns. This will leave this->a pointing to something which does not exist any more (a dangling pointer); any attempts to access *a would cause undefined behavior.

Last edited on
I know your code was only a simplified example, but it was a wrong example.
im trying to change a fields in my class through a function

What memory area is “a” pointing to? Do you really want to assign a value to a not reserved memory area?

The following (ugly) code achieves more or less what you were asking for, but... does it really make any sense?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <limits>

struct A {
    int* a {nullptr};
    int b  {13};
    int* returnA() { a = &b; return a; }
    void change() { int s=10; *returnA() = s; }
};

void waitForEnter();

int main()
{
    A my_a;
    std::cout << "*my_a.returnA(): " << *my_a.returnA()
              << "; my_a.b: " << my_a.b << '\n';
    my_a.change();
    std::cout << "After calling my_a.change(): "
                 "*my_a.returnA(): " << *my_a.returnA()
              << "; my_a.b: " << my_a.b << '\n';
    
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


*my_a.returnA(): 13; my_a.b: 13
After calling my_a.change(): *my_a.returnA(): 10; my_a.b: 10

Press ENTER to continue...

Topic archived. No new replies allowed.