ihm
ihm

Reputation: 1863

invalid initialization of non-const reference

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 ‘&’ ?)

Upvotes: 0

Views: 1026

Answers (4)

johnsyweb
johnsyweb

Reputation: 141810

Once a reference is initialised it cannot be re-initialised or assigned.

It looks like you are trying to do this, using the ternary (or conditional) operator:

const vector<int>& values = (player == 1) ? value1 : value2;

You say:

( "pick" is just an int. )

Your code says:

int pick(int &m);

pick here is a method, not an int.

If you are compiling with g++, I recommend enabling (at least) -Wshadow, which will warn when you make this kind of mistake. You've done the same thing with sum15.

Upvotes: 0

K-ballo
K-ballo

Reputation: 81349

A reference is not assignable, only constructible. You could try doing:

vector<int>& temp = ( player == 1 ) ? this->value1 : this->value2;

Update:

With the updated code, all you have to do is drop the const in temp to 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.

Upvotes: 2

jedwards
jedwards

Reputation: 30210

I think your error is in the vector<int> &temp=vector<int>() line. Its not really clear what you're doing here however, since you attempt to assign &temp and then immediately reassign it in the branches of the if-else statement.

Upvotes: 0

Tony
Tony

Reputation: 10327

A reference cannot be reassigned, you are declaring it and then in your if setting it to a new value.

I don't think you should be trying to use a reference in this case.

Upvotes: 0

Related Questions