I am trying to reference to a class variable which is a vector and change value of the vector. I got this error. what am I doing wrong? thanks in advance. ( “pick” is just an int. )
class tic
{
private:
vector<int> move; //calculate moves
vector<int> value1; //player1's points
vector<int> value2; //player2's points
vector<int> value; //exchange value
vector<string> board; //numbers on the board
public:
void setboard (); //output numbers on the board
void setvalue(); //each number's value corresponding to the numbers on the board
void setvalue12(); //values of player1 and playe2
void set(); //setboard, setvalue, setvalue12
void printboard (int &pick); //print board
int pick(int &m); //pick a number on the board
bool sum15 (vector<int> &sum15); //check if sum is 15 of any combination of 3
int WinLoseDraw (int &pick, int player); //win=0, continue=1, draw=20
void WLD(int &player)
{
vector<int> &temp=(player==1)?this->value1:this->value2;
temp[pick-1]=value[pick-1]; //input values
if (sum15(temp)) //if any sum of 3 is 15
{
cout<<"WINS!"<<endl;
}
}
};
this is the original codes. I am trying to simply this part with a member function or an inline function named WLD
if (player==1)
{
value1[pick-1]=value[pick-1]; //input values
if (sum15(this->value1)) //if any sum of 3 is 15
{
cout<<"PLAYER1 WINS!"<<endl;
return 0;
}
}
else
{
value2[pick-1]=value[pick-1];
if (sum15(this->value2))
{
cout<<"PLAYER2 WINS!"<<endl;
return 0;
}
}
with the updated codes. I got erros on “temp[pick-1]=value[pick-1];
tic.h: In member function ‘void tic::WLD(int&)’:
tic.h:28: error: invalid use of member (did you forget the ‘&’ ?)
tic.h:28: error: invalid use of member (did you forget the ‘&’ ?)
A reference is not assignable, only constructible. You could try doing:
Update:
With the updated code, all you have to do is drop the
constintempto make it work. Note that you have lvalues, the r in rvalues comes not from reference but from right, as in usable on the right side of an expression.