c++ - 错误 : "Function" not declared in this scope

标签 c++ binary-search-tree

我认为这个错误告诉我我的插入函数没有声明但是据我所知我已经正确声明它并且它在我的类的公共(public)部分所以我认为我应该能够在我的主要功能。我试图将其称为 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);

关于c++ - 错误 : "Function" not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43552959/

相关文章:

data-structures - T-trees 与 B+/-trees 相比有哪些优势?

c - 二叉搜索树插入程序显示段错误

c++ - C++ 社区是否就何时应使用异常达成普遍共识?

c++ - 从函数访问 QTextEdit 小部件

java - 从二叉搜索树中返回已删除的节点

java - 使用Java从最大数到最小数打印二叉搜索树

c - 二叉搜索树的删除函数出错

c++ - 文件中存储的对象的字符串成员

c++ - 在这种情况下如何添加计数器?

c++ - 测试 C++ 后缀到中缀堆栈上操作数过多