c++ - 错误 C2661 : 'node::node' : no overloaded function takes 3 arguments

标签 c++ visual-studio-2013 avl-tree

这是一个 AVL 树 C++ 程序,它具有以下资源:

"TreeNode.h"
"AVLTree.h"
"AVLTree.cpp"
"Main.cpp"

我添加了一个“TreeNode.cpp”并从“AVLTree.cpp”中获取“node::node”函数并将其放入“TreeNod.cpp”中,包括“TreeNode.h”并在编译后,VS 2013为该行抛出错误 C2661:

n = new node(x, NULL, NULL);

对应“AVLTree.cpp”错误的函数:

void tree::insert(int x, node* &n)
{
    if (n == NULL)
        n = new node(x, NULL, NULL);

    else if (x < n->data)
    {
        insert(x, n->l);
        if (n->l != NULL && n->r != NULL && n->l->height - n->r->height == 2)
        {
            if (x < n->l->data)
                rightrotation(n);
            else
                doublerotation_leftright(n);
        }
    }
    else if (x > n->data)
    {
        insert(x, n->r);
        if (n->r != NULL && n->l != NULL && n->r->height - n->l->height == 2)
        {
            if (n->r->data < x)
                leftrotation(n);
            else
                doublerotation_leftright(n);
        }
    }
    n->height = maxi(n->l, n->r) + 1;
}

“树节点.cpp”:

#include "TreeNode.h"

node::node(int x, node *newleft, node *newright, int h = 0)
{
    data = x;
    l = newleft;
    r = newright;
    height = h;
}

“AVLTree.h”:

#include "TreeNode.h"

#pragma once

class tree
{
    public:
    tree();
    ~tree();
    void insert(int x);
    bool pop(int n);
    void printpostorder();
    void printlevelorder();

    private:
    node* head;
    node* nullnode;

    void destruct(node* n);
    node* findnode(int n);
    node* min(node* n);
    node* Bfind(int n);

    void rightrotation(node* &k2);
    void leftrotation(node* &k2);
    void doublerotation_leftright(node* &k3);
    void postorder(node* n);
    void levelorder(node* n);
    void insert(int x, node* &n);
    int maxi(node *x1, node *x2);
    void balance(node* &n);
};

问题出在哪里?

编辑 1:

"TreeNode.h"

#pragma once

class node
{
public:
    int data;
    node* l;
    node* r;
    int height;
    node(int x, node* newleft, node* newright, int h);
};

最佳答案

在node的类定义中似乎对应的构造函数没有第四个参数的默认参数。检查类定义并在类定义中的构造函数声明中指定默认参数,而不是在 cpp 文件中的构造函数定义中。

请注意,您可以使用重载委托(delegate)构造函数代替默认参数。例如

class node
{
public:
    node(int x, node *newleft, node *newright);
    node(int x, node *newleft, node *newright, int h);
    //...

//,,,

node::node(int x, node *newleft, node *newright) : node( x, newleft, newright, 0 )
{
}

关于c++ - 错误 C2661 : 'node::node' : no overloaded function takes 3 arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30290620/

相关文章:

c++ - 为什么这个包含 rand() 的 C++11 代码多线程比单线程慢?

c++ - Ret (&)(Args...) 和 Ret (Args...) & 有什么区别?

python - 将 python 命令行工具分发给其他人的最简单方法?

c++ - 销毁整个 AVL 树

c - 插入 AVL 树 : segmentation fault error

C++实现AVL树

c++ - "Click-through"GLFW窗口?

c - 在 VS2013 中调试 GTK+ 3.0 项目时,我在哪里可以读取 g_print 的输出?

c++ - TDD 让我除以零。可以吗?

CSS 格式在 Visual Studio 2013 中不可用