Reputation:
I have searched for this multiple times and still cannot find an answer to this problem. I keep getting an error saying error C2106: '-=' : left operand must be l-value
My code looks like this
case 1:
mItemCost = 4;
if (player.getGold() >= 4){
cout << "You have bought a dagger which replaces your current weapon!" << endl;
player.getGold() -= mItemCost; // Error here
player.getWeapon().mName = "Dagger";
player.getDamageRange().mLow = 1; // Error here
player.getDamageRange().mHigh = 4; // Error here
}
player is a pointer to another class. I would be really grateful for any help. Thank you :D
Upvotes: 0
Views: 103
Reputation: 29243
You are trying to assign a value to a method call, which is illegal unless the return value is a reference.
You probably meant something like
player.setGold(player.getGold() - mItemCost);
Upvotes: 2
Reputation: 75565
You are trying to assign to the return value of a function and based on the error, the function does not return a reference.
You have two options to fix this.
Write (if necessary) and use the appropriate setters instead for each error.
player.setGold(player.getGold() - mItemCost)
Have the getters return references instead.
Consider this simple program which reproduces the error.
#include <stdio.h>
int foo = 0;
int getFoo() {
return foo;
}
int main(){
getFoo() = 5; // error here.
}
We can fix the error by changing the signature of the function to return a reference.
int& getFoo() {
return foo;
}
Note that this is not the idiomatic way to do it, but will work.
Upvotes: 1