title: C++ - Is a class prototype necessary ?
body: <p>Note : Code is posted below.</p>
  
  <p>I want class <code>dynamic array</code> to be able to instantiate the <code>merge_inner</code> class.  So I added a public function like this:</p>
  
  <pre><code>merge_sort()  // merge_sort() is function in class dynamic_array
    {
    merge_inner&lt;T&gt; M1;
    }
  </code></pre>
  
  <p>However this is in one file and merge_inner is defined below dynamic_array.  How do I create a class prototype so that dynamic array know that merge_inner is a real class.</p>
  
  <p>The error given is.</p>
  
  <pre><code>merge_inner not declared in scope
  </code></pre>
  
  <p>Thanks</p>
  
  <pre><code>#include "c_include.cpp"
  
  using namespace std;
  
  template &lt;class T&gt; class dynamic_array
    {
    protected:
      T* array;
    public:
      int size;
      void rorder();
      void order();
      void randorder();
      void print_operator(ostream&amp;)const;
      dynamic_array(int sizein)
        {
        size=sizein;
        array=new T[size]();
        }
      merge_sort()
        {
        merge_inner&lt;T&gt; M1;
        }
    };
  
  template &lt;class T&gt; void dynamic_array&lt;T&gt;::print_operator(ostream &amp;os=cout)const
    {
    for (int i = 0; i &lt; size; i++) os &lt;&lt; array[i] &lt;&lt; endl;
    }
  
  template &lt;class T&gt; void dynamic_array&lt;T&gt;::randorder()
    {
    srand(time(NULL));
    int *ap;
    for(ap=array;ap!=array+size;++ap){*ap=rand();} 
    }
  
  template &lt;class T&gt; void dynamic_array&lt;T&gt;::order()
    {
    int *ap,i=0;
    for(ap=array;ap!=array+size;++ap)                                             
      { 
      *ap=i;
      ++i;        
      } 
    }
  
  template &lt;class T&gt; void dynamic_array&lt;T&gt;::rorder()
    {
    int *ap,i=size-1;
    for(ap=array;ap!=array+size;++ap)                                             
      { 
      *ap=i;
      --i;        
      } 
    }
  
  template&lt;class T&gt; ostream&amp; operator&lt;&lt;(ostream&amp; stream, dynamic_array&lt;T&gt; const&amp; data) 
    { 
    data.print_operator(stream);
    return stream; 
    }   
  
  /*
  Merge Sort
  */
  
  template &lt;class T&gt; class merge_inner : public dynamic_array &lt;T&gt;
    {
    using dynamic_array&lt;T&gt;::array;
    private:
      const static int size;
      int scratch[];
      void flip_if_unordered(int &amp;x, int &amp;y)
        {
        if(array[x]&gt;array[y])
          {
          int tmp=array[x];
          array[x]=array[y];
          array[y]=tmp;
          }
        }
      void merge_algo(int &amp;left, int &amp;right_begin, int &amp;right)
        {
        int iter,iter_left=left,iter_right=right_begin;  
        for(iter=left;iter&lt;=right;++iter)
          {
          if( (iter_right&gt;right) || ((iter_left &lt; right_begin) &amp;&amp; (array[iter_left]&lt;=array[iter_right])))
            {
            scratch[iter]=array[iter_left];
            ++iter_left;
            }
          else
            {
            scratch[iter]=array[iter_right];
            ++iter_right;
            }
          }
        for(iter=left;iter&lt;=right;++iter){array[iter]=scratch[iter];}
        }
      void merge_recurse(int left,int right)
        {
        int left_end=(left+((right-left)/2)); 
        int right_begin=left_end+1;  
        if(((left+1)==right)){flip_if_unordered(left,right);return;}
        else if ((left==right)){return;}
        else
          { 
          merge_recurse(left,left_end);               
          merge_recurse(right_begin,right);  
          merge_algo(left,right_begin,right);
          }   
        }
    public:
      merge_inner()
        {
        scratch = new T[size]();
        if(scratch != NULL){merge_recurse(0, size);}
        }
    };
  
  /*Quick Sort
  
      void quick_sort()
        {
        quick_recurse(0,size);
        }
      void quick_recurse(int left, int right) 
        {  
        int l = left, r = right, tmp;
        int pivot = array[(left + right) / 2];
        while (l &lt;= r)
          {
          while (array[l] &lt; pivot)l++;
          while (array[r] &gt; pivot)r--;
          if (l &lt;= r) 
            {
            tmp = array[l];
            array[l] = array[r];
            array[r] = tmp;
            l++;
            r--;
            }
          }
        if (left &lt; r)quick_recurse(left, r);
        if (l &lt; right)quick_recurse(l, right);
        }  
  */
  </code></pre>