2013-03-03 11 views
0

出於某種原因,我的賦值操作符產生總線錯誤,當我嘗試使用它:重載賦值運算符總線錯誤

hand1 = hand2; 


//overload assignment operator 
Hand Hand::operator=(Hand other_hand) 
{ 

    if(&other_hand != this){ 
     name = other_hand.name; 
     cards = other_hand.cards; 

    } 

    return *this; 
} 

return語句之後

+2

你正在服用並返回副本,你可能不想這樣做。 – 2013-03-03 01:39:39

+1

複製交換!複製交換! – chris 2013-03-03 01:44:01

+0

另外,你能告訴我們一個手的定義嗎?目前還不清楚是否有名字或卡片有需要管理的內存,即你是否在刪除析構函數中加倍刪除內容? – 2013-03-04 01:57:59

回答

1

所有分配首先應該有一個出現的錯誤簽名看起來像這樣:

Hand & Hand::operator=(const Hand &other_hand) 

你可能不想傳遞和返回副本指出而且要允許經營鏈,即:

hand1 = hand2 = hand3 .... 

這是一個basic reference。還提到複製和交換,這previous thread完成解釋它的完美工作。