我想引用一个类变量,它是一个向量并更改向量的值。我有这个错误。我究竟做错了什么?提前致谢。 (“挑”只是一个整数。)非const引用无效初始化
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;
}
}
};
这是原始码。我试图用成员函数或内联函数简单地将该部分命名为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;
}
}
更新后的代码。我有误差修改的“临时[挑-1] =值回升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 ‘&’ ?)
为什么你定义了一个对'vector'的引用? – deepmax
嗨我只是更新代码。只是想简化原始代码。 – ihm