错误:此范围内未声明“功能”

时间:2017-04-21 22:32:16

标签: c++ binary-search-tree

我认为这个错误告诉我,我的插入函数没有声明,但据我所知,我已经正确声明了它并且它在我班级的公共部分,所以我想我应该能够在我的主要功能。我试图将其称为insert(12);,但是它给了我错误:'insert'未在此范围内声明。

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;
    }
}

1 个答案:

答案 0 :(得分:2)

如果您只是编写insert(12);,那么您可能需要创建BST类的实例并将其作为成员函数访问:

BST tree;
tree.insert(12);
相关问题