-1

I am trying to code a template class for b-tree in c++ so it can work with any type char or integer, and i faced this error when i am trying to insert to the tree, in the line 49 , 76 , and 12 so if anyone can help code is below check it out, the code is only for insertion and print only.

template<typename t,int order>
class Node {
    public :
    t *keys;
    int degree; // minimum degree
    Node **children;
    int numberOfKeys;
    bool leaf;
    Node(int index, bool leaf);
    void insertNonFull(t data);
    void splitChild(int i, Node *node);
    void print();
    friend class BTree;
};

   template<typename t,int order>
   class BTree {
   Node<t,order> *root;
   int degree; // minimum degree

  public:BTree() {
  root = NULL;
  this->degree = order;
}
public:void print() {
   if (root != NULL)
    root->print();
}
public:void insert(t data);
  };


template<typename t,int order>
Node<t,order>::Node(int degree, bool leaf) {
this->degree = degree;
this->leaf = leaf;
keys = new int[2 * degree - 1];
children = new Node *[2 * degree];
numberOfKeys = 0;
}

template<typename t,int order>
void BTree<t,order>::insert(t data) {
if (root == NULL) {
    root = new Node<t,order>(degree, true);
    root->keys[0] = data;
    root->numberOfKeys = 1;
  } else {
    if (root->numberOfKeys == 2 * degree - 1) {
        Node<t,order> *newnode = new Node<t,order>(degree, false);

       newnode->children[0] = root;

      newnode->splitChild(0, root);

      int i = 0;
     if (newnode->keys[0] < data)
        i++;
       newnode->children[i]->insertNonFull(data);
       root = newnode;
    } else
     root->insertNonFull(data);
  }
}




int main(){

    BTree<int,3> t;
    t.insert(1);
    t.insert(5);
    t.insert(0);
    t.insert(4);
    t.insert(3);
    t.insert(2);
    t.print();

    return 0;
}

the error i got is in the picture

1 Answers1

1

Add a forward declaration of the BTree before the Node class and swap the friend class BTree; with friend class BTree<t, order>;. Also swap the NULL with nullptrs. It'll solve the issue.

D-RAJ
  • 3,263
  • 2
  • 6
  • 24