title: How store class objects in vector<vector<class *>> and access and pass them to function either by reference or value
body: <pre><code>#ifndef BINARY_TREE_H
  #define BINARY_TREE_H
  
  #include&lt;iostream&gt;
  #include&lt;vector&gt;
  
  using namespace std;
  
  class Binary_Tree;
  
  static int levelCount=0;
  
  extern vector&lt;vector&lt;Binary_Tree*&gt;&gt; vec;
  extern vector&lt;Binary_Tree*&gt; tempVec;
  
  class Binary_Tree
  {
    public:
       Binary_Tree()
       {
          childNum=0;
          data=0;
          level=0;
          prev=NULL;
          next[0]=NULL;
          next[1]=NULL;
      };
  
      Binary_Tree(int d)
      {
           childNum=0;
           data=d;
           level=0;
           prev=NULL;
           next[0]=NULL;
           next[1]=NULL;
           levelCount++;
       }
  
  
      void insert_node(int,int,int);
  
      int get_level();
  
      int get_childCount();
  
      friend int set_childNum(Binary_Tree*);
  
  private:
      int childNum;
      int data;
      int level;
      Binary_Tree *prev;
      Binary_Tree *next[2];
  };
  
  #endif // BINARY_TREE_H
  </code></pre>
  
  <p>Here is the implementation file</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cmath&gt;
  #include "Binary_Tree.h"
  
  using namespace std;
  
  
  
  void Binary_Tree::insert_node(int lev, int d, int sib)
  {
  if(vec.empty())
  {
      cout&lt;&lt;"You Have to create Root first";
  }
  
  else
  {
      if(set_childNum(vec[lev][sib-1])==0)
      {
          cout&lt;&lt;"Child cant be created parent Node already has two childs.";
      }
  
      else
      {
          childNum=set_childNum(vec[lev][sib-1]);
          data=d;
          level=lev+1;
          prev=vec[lev][sib];
          next[0]=NULL;
          next[1]=NULL;
          tempVec.clear();
          for(int i=0; i&lt;pow(2,(lev+1)); i++)
          {
              if(i==childNum-1)
              {
                  tempVec.push_back(this);
              }
              else
              tempVec.push_back(vec[lev][i]);
          }
          vector&lt;vector&lt;Binary_Tree*&gt;&gt;::iterator itr=vec.begin()+(lev+1);
          vec.erase(itr);
          vec.insert(itr,tempVec);
         }
     }
  }
  
  int set_childNum(Binary_Tree *lstNdAdr)
  {
      if(lstNdAdr-&gt;get_childCount()==0)
          return 1;
  
      else if(lstNdAdr-&gt;get_childCount()==1)
          return 2;
  
      else
        return 0;
  }
  
  int Binary_Tree::get_level()
  {
      return level;
  }
  
  int Binary_Tree::get_childCount()
  {
    if(next[0]==NULL)
    {
      return 0;
    }
  
    else if(next[0]!=NULL &amp;&amp; next[1]==NULL)
    {
      return 1;
    }
  
    else
    {
      return 2;
    }
  
  
  }
  </code></pre>
  
  <p>MAIN.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;cmath&gt;
  #include"Binary_Tree.h"
  
  using namespace std;
  
  vector&lt;vector&lt;Binary_Tree*&gt;&gt; vec;
  vector&lt;Binary_Tree*&gt; tempVec;
  
  
  int main()
  {
    Binary_Tree tree;
    here:
    cout&lt;&lt;"Enter your Choice:1.Create Root Of Tree\n"
        &lt;&lt;"2.Insert node\n"&lt;&lt;endl;
  
    int choice;
    cin&gt;&gt;choice;
  
    switch(choice)
    {
      case 1:
        {
          int d;
          cout&lt;&lt;"Enter Data to insert: ";
          cin&gt;&gt;d;
  
           Binary_Tree treeDummy(d);
  
           tree=treeDummy;
  
           tempVec.push_back(&amp;tree);
  
           vec.push_back(tempVec);
         }
  
       break;
  
      case 2:
      {
          int lev;
          int sibbling;
          int d;
          cout&lt;&lt;"Enter at which level and data and parent's sibling-no.: ";
          cin&gt;&gt;lev;
          cin&gt;&gt;d;
          cin&gt;&gt;sibbling;
  
          if(sibbling&gt;pow(2,lev))
              cout&lt;&lt;"Illegal Sibbling Number."&lt;&lt;endl;
          else
              tree.insert_node(lev,d,sibbling);
  
      }
      break;
  }
  int x;
  cin&gt;&gt;x;
  if(x==5)
  {
      cout&lt;&lt;endl&lt;&lt;endl;
      goto here;
  
  }
  
    return 0;
  }
  </code></pre>
  
  <p>in above code i am trying to create a binary tree type structure which can be manipulated  and traversed dynamically that is any node can be inserted and can be removed at run time (although its incomplete because i am stuck at a problem). While pushing back the <code>tempVec</code> vector the code produces a segmentation fault and i am also doubtful in passing the object stored in vetcor> vec to the functions in the implementation (I am new to Stl and first time dealing with vector of vectors containing pointer to the class types)</p>