Bahador
Bahador

Reputation: 75

error C2661: 'node::node' : no overloaded function takes 3 arguments

This is a AVL Tree C++ programm and it has the following resources:

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

I added a "TreeNode.cpp" and took the "node::node" function from "AVLTree.cpp" and put it in "TreeNod.cpp", included the "TreeNode.h" and after compiling, VS 2013 throws me error C2661 for the line:

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

the function that corresponds the error in "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);
};

Where is the problem?

Edit 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);
};

Upvotes: 1

Views: 3739

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

AVLTree.cpp can't see TreeNode.cpp, so it has no idea that the fourth parameter to node's constructor is optional.

Put the default parameter on the declaration, not the definition, so that it is visible in every translation unit that uses the header.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

It seems that in the class definition of node the corresponding constructor does not have the default argument for the fourth parameter. Check the class definition and specify the default argument in the constructor declaration within the class definition instead of the constructor definition within the cpp file.

Take into account that instead of the default argument you could use overloaded delegated constructors. For example

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

Upvotes: 2

Related Questions