0

我面临一个我自己无法解决的小问题。我正在编写一个程序,它在矩阵上实现非常简单的操作。问题是当我试图做这样的事情时:

Matrix first(5);
Matrix e;

first.identityMatrix();
e = first;
cout << first;
cout << e;

简短说明:我想将方阵分配给没有维度的矩阵。

第二个 cout 没有显示任何内容。但是当我将Matrix e()更改为Matrix e(5)时,一切正常。我知道该错误存在于这段代码中:

Matrix& Matrix::operator=(const Matrix& tmp)
{
if (this->a==0 && this->b == 0)
{
    this->matrix = new double*[tmp.a];

    for (int i=0;i<tmp.a;i++)
        this->matrix[i] = new double[tmp.b];
} else {
    try {
        if (this->a!=tmp.a || this->b!=tmp.b)
            throw wrongSize();
    } catch (wrongSize& e) {
        e.message();
        throw;
    }
}

for (int i=0;i<tmp.a;i++)
{
    for (int j=0;j<tmp.b;j++)
    {
        this->matrix[i][j] = tmp.matrix[i][j];
    }
}

return *this;
}

经过一些尝试,我猜想内存分配有问题,但我不确定。对我来说,它应该可以正常工作,因为我返回了对当前对象的引用。我认为构造函数也可能有用:

Matrix::Matrix()
{
a = 0;
b = 0;
matrix = NULL;
}

Matrix::Matrix(int a)
{
try {
    if (a==0)
        throw wrongRowOrColNumber();
} catch (wrongRowOrColNumber& e) {
    e.message();
    throw;
}
this->a = a;
this->b = a;
this->matrix = new double*[a];

for (int i=0;i<a;i++)
    matrix[i] = new double[a];
for (int i=0;i<a;i++)
    for (int j=0;j<a;j++)
        matrix[i][j] = 0;
}

Matrix::Matrix(int a, int b)
{
try {
    if (a==0 || b==0)
        throw wrongRowOrColNumber();
} catch (wrongRowOrColNumber& e) {
    e.message();
    throw;
}

if (a==b)
{
    try {
        if (a==0)
            throw wrongRowOrColNumber();
    } catch (wrongRowOrColNumber& e) {
        e.message();
        throw;
    }
    this->a = a;
    this->b = a;
    this->matrix = new double*[a];

    for (int i=0;i<a;i++)
        matrix[i] = new double[a];
    for (int i=0;i<a;i++)
        for (int j=0;j<a;j++)
            matrix[i][j] = 0;
} else {
    this->a = a;
    this->b = b;
    this->matrix = new double*[a];

    for (int i=0;i<a;i++)
        matrix[i] = new double[b];
    for (int i=0;i<a;i++)
        for (int j=0;j<b;j++)
            matrix[i][j] = 0;
}
}

运算符 <<:

friend ostream& operator<<(ostream& buffer, const Matrix& tmp)
{
    for (int i=0;i<tmp.a;i++)
    {
        for (int j=0;j<tmp.b;j++)
        {
            buffer << tmp.matrix[i][j] << " ";
        }
        buffer << endl;
    }
    return buffer;
};

身份矩阵:

Matrix& Matrix::identityMatrix()
{
try {
    if (this->a!=this->b)
    {
        throw wrongSize();
    }
} catch (wrongSize& e) {
    e.message();
    throw wrongSize();
}
int row = this->a;

for (int i=0;i<row;i++)
{
    for (int j=0;j<row;j++)
    {
        if (i==j)
            this->matrix[i][j] = 1;
        else
            this->matrix[i][j] = 0;
    }
}

return *this;
}
4

1 回答 1

5

You throw an exception several times and catch it immediately after, just to show a message and rethrow. You can save the try/catch, if you just show the message and then throw the exception instead.

In your assignment operator, you must copy the dimensions a and b as well.

Matrix& Matrix::operator=(const Matrix& tmp)
{
    if (this->a==0 && this->b == 0)
    {
        this->a = tmp.a;
        this->b = tmp.b;

        this->matrix = new double*[tmp.a];
        ...
    }
    ...
}

In your constructor Matrix::Matrix(int a, int b), you have an if (a == b) ... else. You can remove the if part and just leave the else part. This way, you have less code and less potential for bugs.

于 2012-11-23T22:02:19.217 回答