2
class bst
{
private:

typedef struct nod
{
  int data;
  nod* left;
  nod* right;
  nod(int key):data(key),left(NULL),right(NULL){}
}node;
 node* root;

public:

void create();
void add(int key,node*curr=root);
void c2ll();
void print(){}

The code doesn't compile... I get the below errors.

ain.cpp: In function ‘int main()’:
main.cpp:7:12: error: call to ‘void bst::add(int, bst::node*)’ uses the default argument for parameter 2, which is not yet defined
   bt.add(50);
            ^
In file included from bst.cpp:1:0:
bst.h:14:8: error: invalid use of non-static data member ‘bst::root’
  node* root;
        ^
bst.h:19:28: error: from this location
 void add(int key,node*curr=root);
                            ^
bst.h:14:8: error: invalid use of non-static data member ‘bst::root’
  node* root;
        ^
bst.cpp:10:34: error: from this location
 void bst::add(int key,node* curr=root)

Any suggestions would be welcome...I am trying to avoid writing a wrapper method and instead use the default functionality provided by c++

basav
  • 1,475
  • 12
  • 20
  • 2
    As the error says you cant set default parameters to non-static member variables. You should overload add to take only the key and call the one taking two parameters with root as the second parameter. – Borgleader Jan 09 '15 at 18:27
  • @Borgleader: You should make that an answer. – jxh Jan 09 '15 at 18:28
  • so, that would be equivalent to writing a wrapper function.Is there anyway I can get away without writing that wrapper function? – basav Jan 09 '15 at 18:32

4 Answers4

4

According to the C++ Standard (8.3.6 Default arguments)

  1. ...Similarly, a non-static member shall not be used in a default argument, even if it is not evaluated, unless it appears as the id-expression of a class member access expression (5.2.5) or unless it is used to form a pointer to member (5.3.1). [ Example: the declaration of X::mem1() in the following example is ill-formed because no object is supplied for the non-static member X::a used as an initializer.
int b;
class X {
int a;
int mem1(int i = a); // error: non-static member a
// used as default argument
int mem2(int i = b); // OK; use X::b
static int b;
};

You could overload function add. For example

void add( int key );

void add( int key, node *curr );

The first function would use root by default. It could simply call the second function passing as the second argument the node root.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • the problem i have is it's a recursive function...void add( int key );....i need the node* in evry stack frame, hence i made it a parameter – basav Jan 09 '15 at 18:39
  • @basav As I have written in my updated post the first function could call the second recursive function.:) So there is no problem. – Vlad from Moscow Jan 09 '15 at 18:40
  • @basav Usually such approach is used for recursive function void reverse( char *s ). The function itself is not recursive but it calls overloaded function void reverse( char *s, size_t n ) like reverse( s, strlen( s ) ); – Vlad from Moscow Jan 09 '15 at 18:48
4

The problem is in the definition for the method:

void add(int key,node*curr=root);

root isn't defined in the context you're using it. If you mean the member variable node* root, there is no way to default to member variables in the member functions, but you could set NULL(0) as the default and check it in the definition.

void bst::add ( int key,node*curr=NULL)
{
     if(curr==NULL) {
         curr= this->root;
     }
}
Etskh
  • 67
  • 6
4

There are two ways.

Either use a "magic" default:

void add(int key, node* curr = NULL)
{
    if (curr == NULL)
        curr = root;
    // ...
}

or ditch the default altogether and use an overload:

void add(int key, node* curr)
{
    // ...
}

void add(int key)
{
    add(key, root);
}

My personal preference is the latter, but you shouldn't really expose the node type in the tree's interface at all, as that would let users of the tree ruin its balancing.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

Use like this: const node* root = NULL; void add(int key,node*curr=root);

You can check the actual running example here: http://ideone.com/tJ1r29

qqibrow
  • 2,942
  • 1
  • 24
  • 40
  • hmm, if the root node is const it won't be possible to, eventually, add another node to the left or right of the root node... – Diego Jan 09 '15 at 19:03