2014-12-02 43 views
0

錯誤是:沒有重載函數「BSTree :: Retrieve」的實例匹配參數列表和對象(對象具有阻止匹配的類型限定符)。引用指針錯誤。沒有重載函數的實例

參數類型有:(intAccount*BSTree::Node*const

對象類型是:const BSTree

它的說法參數類型爲Account *,但我把它當作Account *&acct

第一個參數是要檢索的對象。其次持有指向發現對象的指針。

下面的代碼:

bool BSTree::Retrieve(int ID, Account *&acct, Node *leaf) 
{ 
    if(leaf != NULL) 
    { 
     if(ID == leaf->pAcct->getID()) 
     { 
      acct = leaf->pAcct; 
      return true; 
     } 
     if(ID < leaf->pAcct->getID()) 
     { 
      return Retrieve(ID, acct, leaf->left); 
     } 
     else 
     { 
      return Retrieve(ID, acct, leaf->right); 
     } 
    } 
    else 
    { 
     acct = NULL; 
     return false; 
    } 
} 

bool BSTree::Retrieve(int ID, Account *&acct) const 
{ 
    return Retrieve(ID, acct, root); 
} 
+0

[在C++中傳遞指針的引用](http://stackoverflow.com/questions/823426/passing-references-to-pointers-in-c) – 2015-02-11 15:13:23

回答

0
  1. 要調用從一個const方法的非const方法(該方法可以修改對象)(這是不允許這樣做)。這將是一個錯誤。

  2. 您確定要Account *&?從你的代碼中,如果你想從BSTree的ID中檢索節點,你將需要一個Account*

對於點1 see在這裏。

a.g(); // invokes an error because g() (which is const) calls f() which is non-const. 
0

Account *&acct是指向引用的指針。 這意味着acct將作爲參考指針作爲參數給出。你的函數期望的參數是傳遞一個實際的指針而不是現有的引用。

你可以試試這個:

bool BSTree::Retrieve(int ID, Account *&acct) const 
{ 
    Account* acctPtr = acct; 
    return Retrieve(ID, acctPtr, root); 
} 

你可以看一下類似的情況here

除此之外,您試圖調用非常量方法從const之一。您可以使用const_cast爲了解決這個問題,但它是不是推薦,因爲它是危險的,你應該尋找更好的選擇(看here更多)。

相關問題