受保护的成员不能按预期由基类访问

时间:2011-11-04 21:56:21

标签: c++

来自dynamic_array的派生类merge_sort无权访问受保护的成员T *数组。它们在所使用的任何地方都是错误的。

我不确定为什么......除了merge_sort的公共指示符应该是别的什么?

#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]();
      }
  };
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;
  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_sort : public dynamic_array <T>
  {
  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_sort()
      {
      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);
      }  
*/

1 个答案:

答案 0 :(得分:3)

您的基类依赖于模板参数,因此其类型是依赖类型。在实例化之前,编译器将不知道您使用的基类的哪个特化,因此您必须帮助编译器知道此类标识符是基类的成员。要么是这样的:

dynamic_array<T>::array

this->array

using dynamic_array<T>::array;