简体   繁体   中英

operator overload for dynamic array giving strange error

I'm getting an error regarding "disgarded qualifiers" when I use this. The Entier class is posted below.

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

The global overloaded function:

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

A public member of dynamic_array

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

Entire Class dynamic array:

/*
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;
  }

The problem is in following lines:

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

and

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

Since you are passing data as const& to operator << , you have to maintain its const qualification. ie data cannot call any non- const member of class dynamic_array . You can solve this problem in 2 ways:

  1. pass data as simple dynamic_array<T>& to operator <<
  2. make print_array a const method (uncomment above)

"Discards const qualifiers", presumably...

Replace:

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;
}

...with...

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

...or, better still ...

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

This member function can be declared const since it doesn't modify any attributes of the class. See the FAQ on Const-Correctness .

Your second print_array() method is redundant since the first takes a default argument of std::cout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM