[英]error C2661: 'node::node' : no overloaded function takes 3 arguments
這是一個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);
};
似乎在節點的類定義中,對應的構造函數沒有第四個參數的默認參數。 檢查類定義,並在類定義內的構造函數聲明中指定默認參數,而不是在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 )
{
}
AVLTree.cpp無法看到TreeNode.cpp,因此不知道node
的構造函數的第四個參數是可選的。
將默認參數放在聲明上,而不是定義上,以便在使用標頭的每個翻譯單元中可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.