2011-04-27 18 views
1

所以我試圖學習如何編寫我的第一個BST,這很難....我只用幾行代碼就遇到了麻煩。問題在插入,但我已經包括了一切,以便我可以得到我的風格/其他錯誤的一些反饋。我被建議使用指針實現的指針,但我們還沒有學到它,所以我不覺得舒適/知道如何編寫它。在在第一個BST上插入無結果函數errror?

誤差

cc1plus: warnings being treated as errors 
tree.cpp: In member function âbool Tree::insert(Tree::Node*&, int, std::string)â: 
tree.cpp:34: warning: control reaches end of non-void function 

的tree.h中文件

#ifndef TREE_H 
#define TREE_H 

#include <iostream> 
#include <string> 
using namespace std; 

class Tree 
{ 
public: 
    Tree(); 
    bool insert(int k, string s); 

private: 
    struct Node 
    { 
    int key; 
    string data; 
    Node* left; 
    Node* right; 
    }; 
    Node* root; 
    bool insert(Node*& root, int k, string s); 
}; 

#endif 

tree.cpp

#include <iostream> 
#include "tree.h" 
#include <stack> 
#include <queue> 
#include <string> 
using namespace std; 

Tree::Tree() 
{ 
    root = NULL; 
} 

bool Tree::insert(int k, string s) 
{ 
    return insert(root, k, s); 
} 

bool Tree::insert(Node*& currentRoot, int k, string s) 
{ 
    if(currentRoot == NULL){ 
    currentRoot = new Node; 
    currentRoot->key = k; 
    currentRoot->data = s; 
    currentRoot->left = NULL; 
    currentRoot->right = NULL; 
    return true; 
    } 
    else if (currentRoot->key == k) 
    return false; 
    else if (currentRoot->key > k) 
    insert(currentRoot->left, k, s); 
    else 
    insert (currentRoot->right,k, s); 
} 

movieList.cpp

#include <iostream> 
#include <stack> 
#include <queue> 
#include <string> 
#include "tree.h" 


using namespace std; 

int main() 
{ 
    Tree test; 
    test.insert(100, "blah"); 
    return 0; 
} 
+0

那麼警告是相當的描述,這部分是不是清楚了嗎? – GManNickG 2011-04-27 04:28:51

+0

+1用於'cc1plus:警告被視爲錯誤'。 ;) – Xeo 2011-04-27 04:30:02

回答

4
cc1plus: warnings being treated as errors 
tree.cpp: In member function âbool Tree::insert(Tree::Node*&, int, std::string)â: 
tree.cpp:34: warning: control reaches end of non-void function 

這只是說你不會在每一個可能的路徑上返回一些東西。試試這個:

bool Tree::insert(Node*& currentRoot, int k, string s) 
{ 
    if(currentRoot == NULL){ 
    currentRoot = new Node; 
    currentRoot->key = k; 
    currentRoot->data = s; 
    currentRoot->left = NULL; 
    currentRoot->right = NULL; 
    return true; 
    } 
    else if (currentRoot->key == k) 
    return false; 
    else if (currentRoot->key > k) 
    return insert(currentRoot->left, k, s); 
// ^^^^^^ 
    else 
    return insert (currentRoot->right,k, s); 
// ^^^^^^ 
} 
+0

AH很有道理,非常感謝 – kingcong3 2011-04-27 04:32:26

+0

我覺得自己像個小菜王 – kingcong3 2011-04-27 04:33:36

+0

@ kingcong3:別擔心,每個人都會開始一段時間。必須承認,雖然Visual Studio顯示更易讀的消息:'警告C4715:'插入':並非所有控制路徑都返回一個值' – Xeo 2011-04-27 04:35:27

1

如何:

bool Tree::insert(Node*& currentRoot, int k, string s) 
{ 
    if(currentRoot == NULL){ 
    currentRoot = new Node; 
    currentRoot->key = k; 
    currentRoot->data = s; 
    currentRoot->left = NULL; 
    currentRoot->right = NULL; 
    return true; 
    } 
    else if (currentRoot->key == k) 
    return false; 
    else if (currentRoot->key > k) 
    insert(currentRoot->left, k, s); 
    else 
    insert (currentRoot->right,k, s); 
    return true; 
} 

所有部門需要返回一個值(布爾在這種情況下)。

+0

不要簡單地返回人工值。如果控制權到達您最後的退貨聲明,誰知道該值是否真的被插入? – Xeo 2011-04-27 04:30:58

+0

我也聽說返回多於2個bool值是不好的做法。 – kingcong3 2011-04-27 04:35:04

+0

@ kingcong3那麼,如果你的函數肯定返回一個布爾值,它可以返回false,而在另一個點上它可以返回true。但是,Xeo對於迴歸真實有一個好處。我只是試圖展示如何解決這個錯誤以及它爲什麼起作用。 – Pete 2011-04-27 04:38:50