c++ - Invalid use of non-static data member while using templates -


i have following code snipped, implements binary search tree templates:

#include<iostream> using namespace std;  template<typename t> class node{     public:     t data;     node<t> *left;     node<t> *right;     node<t> *parent;      node(t input_data=null){         data=input_data;         left=null;         right=null;         parent =null;         } };    template<typename t> class binarysearchtree{     private:     long n;     node<t> *root;       public:     binarysearchtree(node<t> *input_root=null, long input_size=0){         n=input_size;         root=input_root;     }       void insert(node<t> *p=root, t  data){         node<t> *par=null;         while(p!=null){             par=p;             if(data <= p->data)                 p=p->left;             else                  p=p->right;         }         node<t> *z=new node<t>(data);         if(root==null){             root=z;             n=1;             return;         }         z->parent=par;         if(data<=par->data)             par->left=z;         else             par->right=z;          n+=1;                     }      void inorder(node<t> *p=root){         if(p){             inorder(p->left);             cout<<p->data<<" ";             inorder(p->right);         }     }  int main(){     binarysearchtree<int> *t=new binarysearchtree<int>();     t->insert(5);     t->insert(15);     t->insert(3);     t->insert(14);     t->insert(25);     t->insert(10);     t->inorder();  } 

there's compilation error on line 27, (i.e. node *root;), , reads: "invalid use of non-static data member 'binarysearchtree::root'". think has default arguments i've included have in functions 'insert' , 'inorder', because don't error when remove default argument 'root'.

my question is, happening , how around it? preserve default argument of root if that's possible.

if matters, i'm using software called 'quincy 2005' on windows 8.1 compile (for reasons).

a default value must literal, not variable name.

additionally, arguments default values must come last in argument list.

rather use:

void insert(node<t> *p, t data) { ... }  // overload 1 argument void insert(t data) {     insert(root, data) } 

Comments