我的框架类中有一个析构函数:
delete this->frameMatrix;
其中 framematrix 属于 Matrix 类,其中 this 作为构造函数和析构函数:
// Constructor: Initialize matrix & sizes
Matrix::Matrix(int width, int height)
{
table = new double* [height];
for(int i = 0; i < height; i++)
table[i] = new double [width];
// Set all values to zero
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
table[row][col] = 0;
}
}
this->width = width;
this->height = height;
}
// Destructor: delete matrix
Matrix::~Matrix()
{
for(int row = 0; row < height; row++)
delete [] table[row];
delete [] table;
this->width = 0;
this->height = 0;
}
当在 frameMatrix 上调用 delete 时,程序在矩阵的析构函数中给出了一个断言失败。
我做错了什么,因为我没有看到如何删除二维双数组的问题。
编辑:
复制构造函数:
Matrix::Matrix(const Matrix &m)
{
this->height = m.getHeight();
this->width = m.getWidth();
this->table = new double* [height];
for(int i = 0; i < height; i++)
this->table[i] = new double [width];
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
this->table[row][col] = m.table[row][col];
}
}
}
我的重载=
Matrix &operator = (const Matrix &m)
{
this->height = m.getHeight();
this->width = m.getWidth();
this->table = new double* [height];
for(int i = 0; i < height; i++)
this->table[i] = new double [width];
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
this->table[row][col] = m.table[row][col];
}
}
}