这是一个 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/