C ++ - 是否需要类原型?

时间:2011-11-04 22:31:36

标签: c++ class prototype

注意:代码发布在下面。

我希望类dynamic array能够实例化merge_inner类。所以我添加了一个这样的公共函数:

merge_sort()  // merge_sort() is function in class dynamic_array
  {
  merge_inner<T> M1;
  }

但是这是在一个文件中,merge_inner定义在dynamic_array下面。如何创建类原型,以便动态数组知道merge_inner是一个真正的类。

给出的错误是。

merge_inner not declared in scope

由于

#include "c_include.cpp"

using namespace std;

template <class T> class dynamic_array
  {
  protected:
    T* array;
  public:
    int size;
    void rorder();
    void order();
    void randorder();
    void print_operator(ostream&)const;
    dynamic_array(int sizein)
      {
      size=sizein;
      array=new T[size]();
      }
    merge_sort()
      {
      merge_inner<T> M1;
      }
  };

template <class T> void dynamic_array<T>::print_operator(ostream &os=cout)const
  {
  for (int i = 0; i < size; i++) os << array[i] << endl;
  }

template <class T> void dynamic_array<T>::randorder()
  {
  srand(time(NULL));
  int *ap;
  for(ap=array;ap!=array+size;++ap){*ap=rand();} 
  }

template <class T> void dynamic_array<T>::order()
  {
  int *ap,i=0;
  for(ap=array;ap!=array+size;++ap)                                             
    { 
    *ap=i;
    ++i;        
    } 
  }

template <class T> void dynamic_array<T>::rorder()
  {
  int *ap,i=size-1;
  for(ap=array;ap!=array+size;++ap)                                             
    { 
    *ap=i;
    --i;        
    } 
  }

template<class T> ostream& operator<<(ostream& stream, dynamic_array<T> const& data) 
  { 
  data.print_operator(stream);
  return stream; 
  }   

/*
Merge Sort
*/

template <class T> class merge_inner : public dynamic_array <T>
  {
  using dynamic_array<T>::array;
  private:
    const static int size;
    int scratch[];
    void flip_if_unordered(int &x, int &y)
      {
      if(array[x]>array[y])
        {
        int tmp=array[x];
        array[x]=array[y];
        array[y]=tmp;
        }
      }
    void merge_algo(int &left, int &right_begin, int &right)
      {
      int iter,iter_left=left,iter_right=right_begin;  
      for(iter=left;iter<=right;++iter)
        {
        if( (iter_right>right) || ((iter_left < right_begin) && (array[iter_left]<=array[iter_right])))
          {
          scratch[iter]=array[iter_left];
          ++iter_left;
          }
        else
          {
          scratch[iter]=array[iter_right];
          ++iter_right;
          }
        }
      for(iter=left;iter<=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 <= r)
        {
        while (array[l] < pivot)l++;
        while (array[r] > pivot)r--;
        if (l <= r) 
          {
          tmp = array[l];
          array[l] = array[r];
          array[r] = tmp;
          l++;
          r--;
          }
        }
      if (left < r)quick_recurse(left, r);
      if (l < right)quick_recurse(l, right);
      }  
*/

2 个答案:

答案 0 :(得分:2)

将您的声明和实施分开。

dynamic_array中,请输入以下代码:

// Declare, but don't implement yet
dynamic_array(int sizein);
merge_sort();

然后在你的文件中进一步向下,在声明merge_inner之后,执行先前声明的函数:

dynamic_array::dynamic_array(int sizein)
{
  size=sizein;
  array=new T[size]();
}
dynamic_array::merge_sort()
{
  merge_inner<T> M1;
}

它们甚至可以位于文件末尾的#included单独文件中。 这样,您的界面易于阅读,并且您的实现被分离出来。

答案 1 :(得分:1)

在定义merge_inner之前,您必须添加dynamic array模板的转发声明:

template <typename> class merge_inner;

请注意,即使模板尚未定义,您也可以声明merge_inner<T> M1;。这是因为dynamic_array本身就是一个模板,只有在外部模板本身被实例化时才需要完整的类型merge_inner<T>