重载<<时第二个参数const类型时遇到问题,求救!
//问题出在CArray2D.h中,当重载<<时,第二个参数改为const类型编译有问题,应该怎么改才能使第二个参数可以用const
//这是CArray.h头文件
#ifndef CLASS_ARRAY
#define CLASS_ARRAY
#include <ostream>
using namespace std;
template<class T>
class Array
{
protected:
T *data;
unsigned int base;
unsigned int length;
public:
Array();
Array(unsigned int, unsigned int = 0);
~Array();
Array(Array const&);
Array& operator = (Array const&);
T const& operator [] (unsigned int) const;
T& operator [] (unsigned int);
T const* Data() const;
unsigned int Base() const;
unsigned int Length() const;
void SetBase(unsigned int);
void SetLength(unsigned int);
};
template <class T>
Array<T>::Array():data(new T[0]),base(0),length(0){}
template <class T>
Array<T>::Array(unsigned int n, unsigned int m/* = 0 */):data(new T[n]),base(m),length(n){}
template <class T>
Array<T>::Array(Array<T> const& array):data(new T [array.length]),base(array.base),length(array.length)
{
for(unsigned int i = 0; i < length; i++)
data[i] = array.data[i];
}
template <class T>
Array<T>::~Array()
{
delete [] data;
}
template <class T>
T const* Array<T>::Data() const
{
return data;
}
template <class T>
unsigned int Array<T>::Base() const
{
return base;
}
template <class T>
unsigned int Array<T>::Length() const
{
return length;
}
template <class T>
T const& Array<T>::operator [](unsigned int position) const
{
unsigned int const offset = position - base;
if(offset >= length)
throw out_of_range("invalid position");
return data[offset];
}
template <class T>
T& Array<T>::operator [](unsigned int position)
{
unsigned int const offset = position - base;
if(offset >= length)
throw out_of_range("invalid position");
return data[offset];
}
template <class T>
void Array<T>::SetBase(unsigned int newBase)
{
base = newBase;
}
template <class T>
void Array<T>::SetLength(unsigned int newLength)
{
T* const newData = new T[newLength];
unsigned int const min = length < newLength?length : newLength;
for(unsigned int i = 0; i < min; i++)
newData[i] = data[i];
delete [] data;
data = newData;
length = newLength;
}
#endif
//这是CArray2D.h头文件
#ifndef CLASS_ARRAY2D
#define CLASS_ARRAY2D
#include "CArray.h"
#include <iostream>
using namespace std;
template <class T>
class Array2D
{
protected:
unsigned int numberOfRows;
unsigned int numberofColumns;
Array<T> array;
public:
class Row
{
Array2D& array2D;
unsigned int const row;
public:
Row(Array2D& _array2D,unsigned int _row):array2D(_array2D),row(_row){}
T& operator [] (unsigned int colum)
{
return array2D.Select(row,colum);
}
};
Array2D(unsigned int,unsigned int);
T& Select(unsigned int, unsigned int);
Row operator [] (unsigned int row)
{
return Row(*this,row);
}
template <class T>
friend ostream& operator << (ostream&Array2D_out, Array2D<T>&arg);
template <class T>
friend istream& operator >> (istream&Array2D_in, Array2D<T>&arg);
};
template <class T>
Array2D<T>::Array2D(unsigned int m,unsigned int n):numberOfRows(m),numberofColumns(n),array(m*n)
{
for (unsigned int i = 0; i < numberOfRows; i++)
{
for(unsigned int j = 0; j < numberofColumns; j++)
(*this)[i][j] = 0;
}
}
template <class T>
T& Array2D<T>::Select(unsigned int i, unsigned int j)
{
if(i > numberOfRows)
throw out_of_range("invalid row!");
if(j > numberofColumns)
throw out_of_range("invalid colum!");
return array[i * numberofColumns + j];
}
template <class T>
istream& operator >> (istream& Array2D_in, Array2D<T>& arg)
{
for (unsigned int i = 0; i < arg.numberOfRows; i++)
{
for(unsigned int j = 0; j < arg.numberofColumns; j++)
Array2D_in >> arg[i][j];
}
return Array2D_in;
}
template <class T>
ostream& operator << (ostream& Array2D_out, Array2D<T>& arg)
{
for (unsigned int i = 0; i < arg.numberOfRows; i++)
{
for(unsigned int j = 0; j < arg.numberofColumns; j++)
Array2D_out << arg[i][j] << " ";
Array2D_out<< endl;
}
return Array2D_out;
}
#endif
//这是源文件main
#include <iostream>
#include "CArray2D.h"
using namespace std;
int main()
{
Array2D<int> matrics(2,2);
cin >> matrics;
cout << matrics;
system("pause");
return 0;
}