动态数组的运算符重载给出奇怪的错误

时间:2011-11-02 05:02:47

标签: c++ stream compiler-errors

当我使用它时,我收到关于“反感限定词”的错误。 Entier类发布在下面。

cout << d // where d is of type dynamic_array.

全局重载功能:

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

dynamic_array的公共成员

void print_array(std::ostream &os = cout) 
  { 
  for (int i = 0; i < size; i++) os << array[i] << endl; 
  } 

整个类动态数组:

/*
Needs a reszie function added
Merge sort is better for sequential, stable(equal elements not re-arranged, or
*/
#include "c_arclib.cpp"

using namespace std;

template <class T> class dynamic_array
  {
  private:
    T* array;
    T* scratch;
    void merge_recurse(int left, int right)
      {
      if(right == left + 1)
        {
        return;
        }
      else
        {
        int i = 0;
        int length = right - left;
        int midpoint_distance = length/2;
        int l = left, r = left + midpoint_distance;
        merge_recurse(left, left + midpoint_distance);
        merge_recurse(left + midpoint_distance, right);
        for(i = 0; i < length; i++)
          {
          if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
            {
            scratch[i] = array[l];
            l++;
            }
          else
            {
            scratch[i] = array[r];
            r++;
            }
          }
        for(i = left; i < right; i++)
          {
          array[i] = scratch[i - left];
          }
        }
      }
    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);
      }  
  public:
    int size;
    dynamic_array(int sizein)
      {
      size=sizein;
      array = new T[size]();
      }
    void print_array(std::ostream &os = cout) 
      { 
      for (int i = 0; i < size; i++) os << array[i] << endl; 
      } 
    void print_array()
      {
      for (int i = 0; i < size; i++) cout << array[i] << endl;
      }
    int merge_sort()
      {
      scratch = new T[size]();
      if(scratch != NULL)
        {
        merge_recurse(0, size);
        return 1;
        }
      else
        {
        return 0;
        }
      }
    void quick_sort()
      {
      quick_recurse(0,size);
      }
    void rand_to_array()
      {
      srand(time(NULL));
      int* k;
      for (k = array; k != array + size; ++k)                                             
        { 
        *k=rand();                                      
        } 
      }
    void order_to_array()
      {
      int* k;
      int i = 0;
      for (k = array; k != array + size; ++k)                                             
        { 
        *k=i;
        ++i;        
        } 
      }
    void rorder_to_array()
      {
      int* k;
      int i = size;
      for (k = array; k != array + size; ++k)                                             
        { 
        *k=i;
        --i;        
        } 
      }
  };


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

int main()
  {
  dynamic_array<int> d1(1000000);
  d1.order_to_array();
  clock_t time_start=clock();
  d1.merge_sort(); 
  clock_t time_end=clock();
  double result = (double)(time_end - time_start) / CLOCKS_PER_SEC; 
  cout << result;
  cout << d1;
  }

2 个答案:

答案 0 :(得分:4)

问题在于以下几行:

template <class T>
std::ostream& operator<<(std::ostream& stream, dynamic_array<T> const& data) 
                                                                ^^^^^^

void print_array(std::ostream &os = cout)  /* const */
                                              ^^^^^  missing

由于您将data作为const&传递给operator <<,因此您必须保持其const资格。即data无法呼叫const的任何非class dynamic_array成员。您可以通过两种方式解决此问题:

  1. data简单dynamic_array<T>&传递给operator <<
  2. 使print_array成为const方法(取消上述注释)

答案 1 :(得分:2)

“丢弃const限定符”,大概是......

替换:

void print_array(std::ostream &os = cout)
{
    for (int i = 0; i < size; i++) os << array[i] << endl;
}
void print_array()
{
    for (int i = 0; i < size; i++) cout << array[i] << endl;
}

...与...

void print_array(std::ostream &os = cout) const
{
    for (int i = 0; i < size; i++) os << array[i] << endl;
}

...或者,更好的......

void print_array(std::ostream &os = cout) const
{
    std::copy(array, array + size, std::ostream_iterator<T>(os, "\n"));
}

此成员函数可以声明为const,因为它不会修改类的任何属性。请参阅FAQ on Const-Correctness

您的第二个print_array()方法是多余的,因为第一个方法采用默认参数std::cout