错误:"Function"未在此范围内声明

Error: "Function" not declared in this scope

本文关键字:范围内 声明 Function 错误      更新时间:2023-10-16

我认为这个错误告诉我我的插入功能没有声明,但是据我所知,我的插入功能正确地声明了,并且它在我的班级的公共部分,所以我认为我应该能够在我的主要功能中使用它。我试图将其称为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;
    }
}

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

BST tree;
tree.insert(12);