我在为具有私有二维动态数组的模板类创建析构函数时遇到问题。出于某种原因,一旦我将信息输入到矩阵中,析构函数就会破坏矩阵。不知道出了什么问题,因为它编译得很好,但是当我输入前 2 个矩阵的信息并且程序尝试将它们相乘时给出错误。如果我摆脱了析构函数,代码就可以工作。
template <class T>
class matrix
{
//sudo
friend matrix operator + , *,-,(bunch of friends used to overload)
//end sudo
public:
matrix(): rows(0), cols(0){}
int Arows(){return rows;}
int Acols(){return cols;}
class Proxy
{
matrix& _a;
int _i;
public:
Proxy(matrix& a, int i) : _a(a), _i(i){}
int& operator[](int j) {return _a.Array[_i][j];};
};
Proxy operator[](int i) {return Proxy(*this,i);}
~matrix();
private:
T ** Array;
int rows;
int cols;
};
template<class T>
matrix<T>::~matrix()
{
for (int i=0;i<rows;i++)
delete [] Array[i];
delete [] Array;
}