重载<<时第二个参数const类型时遇到问题,求救!

wumuzi 2010-12-12 10:03:02
//问题出在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;
}
...全文
113 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wumuzi 2010-12-13
  • 打赏
  • 举报
回复
调用方法改为getNumberOfRows还是不行,问题出在重载[][]上面,但我不知道该怎么去解决!望各位赐教!
就想叫yoko 2010-12-12
  • 打赏
  • 举报
回复
+1,<<对第二个参数没有const不const的限制[Quote=引用 1 楼 wyget 的回复:]
arg.numberOfRows

这句错了
不能访问const里面的成员
解决:
为numberOfRows设置get方法,调用get方法获取numberOfRows
[/Quote]
wyget 2010-12-12
  • 打赏
  • 举报
回复
arg.numberOfRows

这句错了
不能访问const里面的成员
解决:
为numberOfRows设置get方法,调用get方法获取numberOfRows

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧