2015-10-09 22 views
-3

我有一個函數的代碼,其目的是解密由字符串r,0和1給出的消息。這個想法是我有一個BST,並且基於我的r 0和1的字符串,我將能夠找到我要查找的特定數據的路徑。模板BST方法的返回值問題

我根本找不出要返回什麼來匹配函數的返回值,並從我正在尋找的函數中獲取信息!這裏是教授給的幫助...

/*The decrypt() method takes an encrypted string (or path through the tree) 
in the form provided by encrypt(). It should return a pointer to the 
associated string for the given path (or NULL if the path is invalid).*/ 

任何人都可以幫助我通過如何返回正確的變量類型和值,並解釋原因嗎?謝謝! (下面的代碼)

template<class Base> 
const Base * EncryptionTree<Base>::decrypt(const string & path) const{ 
    const BSTNode<Base>* temp = root; 
    bool flag = false; 
    for (int i = 0; i < path.size(); i++) { 
     switch (path[i]) { 
     case 'r': 
      if (temp != NULL) { 
       temp = root; 
      } 
      else { 
       flag = true; 
      } 
      break; 
     case '0': 
      if (temp->getLeft() != NULL) { 
       temp = temp->getLeft(); 
      } 
      else { 
       flag = true; 
      } 
      break; 
     case '1': 
      if (temp->getRight() != NULL) { 
       temp = temp->getRight(); 
      } 
      else { 
       flag = true; 
      } 
      break; 
     } 
    } 

    return ?????; 
} 

我試圖返回*溫度,溫度,TEMP->的getData()(返回一個const基地&)等

+0

「我有麻煩」不是一個適當的問題描述。 –

+0

感謝您的意見,您對我的問題的解決方案有任何建議嗎? –

+0

請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) – GingerPlusPlus

回答

0

我可能是錯的(因爲我沒看到

if (temp != NULL) 
    return &temp->getData(); 
else 
    return NULL; 

所以,如果溫度是不爲空,你用」:基地和BSTNode),但如果TEMP->的getData()返回常量基地&,那麼你的解密函數的結束可能是這樣的聲明重新使用getData()以獲取此節點包含的對象(在您的情況字符串中)的引用。然後你將這個引用綁定的對象的地址傳回給調用者。