【问题标题】:error C2661: 'node::node' : no overloaded function takes 3 arguments错误 C2661:'node::node':没有重载函数需要 3 个参数
【发布时间】:2015-05-17 18:34:18
【问题描述】:

这是一个 AVL Tree 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;
}

“TreeNode.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);
};

【问题讨论】:

  • 添加到帖子末尾...
  • 我在参数中添加了一个“0”,代码编译成功! n = new node(x, NULL, NULL, 0); 不确定我所做的是否有意义!

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


【解决方案1】:

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

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

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 )
{
}

【讨论】:

    【解决方案2】:

    AVLTree.cpp看不到TreeNode.cpp,所以不知道node的构造函数的第四个参数是可选的。

    将默认参数放在声明上,而不是定义上,这样它在每个使用标题的翻译单元中都是可见的。

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 2021-07-05
      • 1970-01-01
      • 2021-03-19
      • 2017-05-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多