2011-11-06 55 views
0

我想引用一个类变量,它是一个向量并更改向量的值。我有这个错误。我究竟做错了什么?提前致谢。 (“挑”只是一个整数。)非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 ‘&’ ?) 
+2

为什么你定义了一个对'vector'的引用? – deepmax

+0

嗨我只是更新代码。只是想简化原始代码。 – ihm

回答

2

引用不可赋值,仅可构造。你可以尝试这样做:

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

更新:

有了更新的代码,你需要做的是在下降的tempconst使其工作。请注意,您必须左值,在[R右值不是来自参考,在使用表达式的右侧。

+0

请注意,这只适用于如果this-> value1()和this-> value2()返回左值,因为您不能将右值绑定到非常量引用。 – je4d

+0

value1和value2存储int数字 – ihm

+1

然后你必须使温度成为一个真实的'vector '而不是参考,即'vector temp((player == 1)?this-> value1():this-> VALUE2());'。请注意,这样做会导致向量被复制,但是如果您想让temp为非const并且value1()/ value2()返回值 – je4d

0

,我认为你的错误是在vector<int> &temp=vector<int>()行其真不明白,但是你在这里做什么,因为你尝试给&temp,然后立即在if-else语句的分支重新分配它。

+0

我想要做的是将this-> value1或this-value2赋值给一个临时变量。如果n == 1,那么temp = this-> value1。其他temp = this-> value2。并使用temp更改value1或value2的值。 – ihm

+0

如果'this-> value1'和'this-> value2'已经是矢量,并且你想要引用其中的一个以后使用,那么上面的k-ballo的答案应该完全按照你想要的来做。 – jedwards

+0

我更新了代码。 @jedwards – ihm

0

的引用不能被重新分配,你声明,然后在你的if将其设置为一个新值。

我不要以为在这种情况下你应该尝试使用一个参考。

+0

我应该在这种情况下使用什么?在此先感谢 – ihm

+0

@ihm - 您必须在声明时指定参考。之后,您不能重新引用另一个对象(http://www.parashift.com/c++-faq-lite/references.html#faq-8.5)。看看K-ballo的答案,这可能是目前最简单的代码修复方法,无需重新构建它。尽管我建议你考虑一下代码结构,为什么你要分开存储一个玩家的分数,为什么不在游戏里面有一个'Player'对象? – Tony

+0

非常感谢。我现在明白了。我应该在这种情况下使用什么? – ihm

0

参考初始化后,不能重新初始化或分配。

它看起来像你试图做到这一点,利用the ternary (or conditional) operator

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

你说:

("pick" is just an int.)

您的代码表示:

int pick(int &m); 

pick这里是方法,而不是int

如果您使用g++进行编译,我建议启用(至少)-Wshadow,这会在您犯这类错误时发出警告。您使用sum15完成了相同的操作。

+0

它给了我上面的代码这个错误。 tic.h:在成员函数'void tic :: WLD(int&)'中: tic.h:27:错误:不匹配调用'(std :: vector >)() ' tic.h:27:错误:不匹配呼叫'(std :: vector >)()' tic.h:28:错误:无效使用成员忘了'&'?) tic.h:28:错误:无效使用成员(你忘了'&'?) tic.h:29:错误:没有匹配函数调用'tic :: sum15 (const std :: vector >&)' tic.h:23:note:candidates:bool tic :: sum15(std :: vector >& ) – ihm

+0

这里有两个问题。 1:你有太多标识符叫'pick'。2:'sum15()'可能需要一个'const'引用时需要一个非'const'引用。 – Johnsyweb

+0

谢谢,约翰。我正在改变他们。你有目标,谷歌聊天或其他? – ihm

相关问题