2017-04-21 110 views
1

我認爲這個錯誤告訴我,我的插入函數沒有被聲明,但據我所知我已經正確聲明瞭它,並且它在我的類的公共部分中我想我應該可以在我的主要功能中使用它。我試圖把它稱爲insert(12);,但它給了我錯誤:'插入'未在此範圍內聲明。錯誤:在此範圍內未聲明「函數」

class BST 
{ 
    public: 
     BST(); 
     BST(int* arr, int size); 
     void insert(int val); 
     void inOrderTraversal(); 
     void inOrderTraversal(node * Root); 

    private: 
     node * Root; 

}; 

void BST::insert(int val) 
{ 
    node* temp = new node(); 
    temp->value = val; 

    if(Root == NULL) { 
     Root = temp; 
     return; 
    } 

    node* current; 
    current = Root; 
    node* parent; 
    parent = Root; 
    current = (temp->value < current->value) ? (current->Left) : (current->Right); 

    while(current != NULL) 
    { 
     parent = current; 
     current = (temp->value < current->value) ? (current->Left) : (current->Right); 
    } 

    if(temp->value < parent->value) { 
     parent->Left = temp; 
    } 

    if(temp->value > parent->value) { 
     parent->Right = temp; 
    } 
} 
+0

你在哪裏打電話嗎? – tkausl

+0

在您的'main()'函數中添加部件,其中包含'insert(12)'到您的文章。 –

回答

2

如果你只是寫insert(12);,那麼你可能需要創建BST類的實例和訪問它作爲一個成員函數:

BST tree; 
tree.insert(12); 
+0

杜,謝謝。 –

+0

@Garrett如果這解決了您的問題,請將其標記爲,謝謝。 – nitronoid