简体   繁体   English

矩阵类和运算符=重载

[英]Matrix Class and operator= overloading

I am facing a little problem that I can't solve on my own. 我面临着一个我自己无法解决的小问题。 I am writing a program which implements very simple operations on matrixes. 我正在编写一个对矩阵执行非常简单的操作的程序。 The problem is that when I am trying to do something like this: 问题是,当我尝试执行以下操作时:

Matrix first(5);
Matrix e;

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

Short explanation: I want to assign square matrix to a matrix without dimensions. 简短说明:我想将正方形矩阵分配给没有尺寸的矩阵。

Second cout doesn't show anything. 二分球什么也没显示。 But when I change Matrix e() to Matrix e(5) , everything works perfectly. 但是,当我将Matrix e()更改为Matrix e(5)时 ,一切正常。 I know that the bug resides in this piece of code: 我知道错误存在于这段代码中:

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;
}

After some attempts I guess that something is wrong with memory allocation, but I am not sure. 经过一些尝试,我猜想内存分配有问题,但是我不确定。 To me it ought to work correctly due to the fact that I return the reference to the current object. 对我来说,由于我将引用返回到当前对象,因此它应该可以正常工作。 I think that constructors might be useful as well: 我认为构造函数也可能有用:

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;
}
}

Operator <<: 运算符<<:

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;
};

IdentityMatrix: IdentityMatrix:

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;
}

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. 如果只显示消息,然后抛出异常,则可以保存try/catch

In your assignment operator, you must copy the dimensions a and b as well. 在分配运算符中,还必须复制尺寸ab

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 . 在构造函数Matrix::Matrix(int a, int b) ,您有一个if (a == b) ... else You can remove the if part and just leave the else part. 您可以删除if部分,而只保留else部分。 This way, you have less code and less potential for bugs. 这样,您的代码更少,潜在的错误更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM