C++运算符重载矩阵运算的问题

时间:2022-10-19 17:34:24
这个程序我写了好久啊。实在没有头绪了。
要求用C++编写一个可以实现矩阵运算符+、-和*的重载的程序。
我现在遇到的问题是程序在运行到打印经过加法和减法的运算后无法显示矩阵。
我尝试了一下调试,发现好像是指针的问题,又像是返回对象是拷贝构造函数和析构函数的问题。
我实在是想破头都找不出原因。
希望大家帮我看看,指出我的问题所在。

类的成员:
矩阵的长和宽,用指针动态申请的二维数组。
类的方法:
初始化的构造函数、拷贝构造函数、析构函数
给矩阵即二维数组赋值的函数、打印矩阵的函数
操作符重载+、-
我还不是很清楚应该怎么重载*,麻烦大家给我指点指点。

谢谢了!!

矩阵的类头文件:

#ifndef MATRIX_H
#define MATRIX_H

class Matrix
{
private:
int width;
int length;
int* pMatrix;

public:
Matrix();
Matrix(int Width,int Length);
Matrix(const Matrix &m);
~Matrix();

void Initialize();
void Display();

Matrix& operator +(const Matrix &m);
Matrix& operator -(const Matrix &m);
};

#endif 


矩阵类的实现文件

#include <iostream>
#include <cstdlib>
#include "Matrix.h"
using namespace std;

Matrix::Matrix()
{
width=length=0;
pMatrix=NULL;
}

Matrix::Matrix(int Width,int Length)
:width(Width),length(Length)
{
pMatrix=new int [width*length];
if(!pMatrix)
{
cout<<"Error!Fail to allocate space.";
abort();
}
}

Matrix::Matrix(const Matrix &m)
{
width=m.width;
length=m.length;
if(!m.pMatrix)
{
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(pMatrix+i*width+j)=*(m.pMatrix+i*width+j);
}
}
}
}

Matrix::~Matrix()
{
delete [] pMatrix;
pMatrix=NULL;
}

void Matrix::Initialize()
{
for(int i=0;i<width*length;i++)
{
cin>>*(pMatrix+i);
}
}



void Matrix::Display()
{
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
cout<<*(pMatrix+i*width+j)<<"  ";
}
cout<<endl;
}
cout<<endl;
}

Matrix& Matrix::operator +(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<endl;
abort();
}
else
{
Matrix t(this->width,this->length);
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(t.pMatrix+i*width+j)=*(m.pMatrix+i*width+j)+*(this->pMatrix+i*width+j);
}
}
return t;
}
}

Matrix& Matrix::operator -(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<endl;
abort();
}
else
{
Matrix t(this->width,this->length);
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(t.pMatrix+i*width+j)=*(this->pMatrix+i*width+j)-*(m.pMatrix+i*width+j);
}
}
return t;
}
}



主函数
#include <cstdlib>
#include "Matrix.h"
using namespace std;

void main()
{
int p,q;
cin>>p>>q;
Matrix m1(p,q);
Matrix m2(p,q);

m1.Initialize();
m2.Initialize();

m1.Display();
m2.Display();

Matrix m3=m1+m2;
Matrix m4=m1-m2;

m3.Display();
m4.Display();
}

12 个解决方案

#1


该回复于2011-05-03 09:18:47被版主删除

#2




Matrix& Matrix::operator -(const Matrix &m)
{
    if(width!=m.width||length!=m.length)
    {
        cout<<"Error!Can't run the operation."<<endl;
        abort();
    }
    else
    {
        Matrix t(this->width,this->length);    
        int i,j;
        for(i=0;i<width;i++)
        {
            for(j=0;j<length;j++)
            {
                *(t.pMatrix+i*width+j)=*(this->pMatrix+i*width+j)-*(m.pMatrix+i*width+j);
            }
        }
        return t;
    }
}

#3


引用 2 楼 newstudent_never 的回复:
C/C++ code


Matrix&amp; Matrix::operator -(const Matrix &amp;m)
{
    if(width!=m.width||length!=m.length)
    {
        cout<<"Error!Can't run the operation."<<endl;
        abort();
    }
……


又返回的是栈变量, 清栈后调用!!!!!

#4


矩阵乘法会么lz?

会的话应该没问题,加减lz都会了,乘也so easy

#5


Matrix t(this->width,this->length);    
你这句话就多余,  直接用 this指针做 运算, 然后返回 return *this 不好吗??
非得返回  生命周期马上结束的 栈中变量

#6


引用 5 楼 newstudent_never 的回复:
Matrix t(this->width,this->length);  
你这句话就多余, 直接用 this指针做 运算, 然后返回 return *this 不好吗??
非得返回 生命周期马上结束的 栈中变量


运行程序时崩溃的原因就是在这里吗?还有没有什么地方有问题?

#7


关于运算符重载,什么时候重载成成员函数?什么时候重载成友元函数比较好骄傲?可以给个代码例子吗?

#8


你没看到 warning 吗? 返回局部变量的引用了.
改成
Matrix operator +(const Matrix &m);
Matrix operator -(const Matrix &m);

operator+ 这类操作符不应该返回引用.
包括什么时候用成员,什么时候用友元可参考
http://www.adintr.com/myarticle/operator.html

#9


引用 8 楼 adlay 的回复:
你没看到 warning 吗? 返回局部变量的引用了.
改成
Matrix operator +(const Matrix &amp;m);
Matrix operator -(const Matrix &amp;m);

operator+ 这类操作符不应该返回引用.
包括什么时候用成员,什么时候用友元可参考
http://www.adintr.com/myarticle/oper……


我改了,类声明里运算符重载时返回对象,
但是我的程序还是出现问题。
但我试图让主函数在昨晚加法和减法后打印矩阵m3 m4的时候就被系统终止了。
Matrix Matrix::operator +(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<endl;
abort();
}
else
{
Matrix t(width,length);
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(t.pMatrix+i*width+j)=*(m.pMatrix+i*width+j)+*(this->pMatrix+i*width+j);
}
}
return t;
}
}

Matrix Matrix::operator -(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<endl;
abort();
}
else
{
Matrix t(width,length);
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(t.pMatrix+i*width+j)=*(this->pMatrix+i*width+j)-*(m.pMatrix+i*width+j);
}
}
return t;
}
}

#10


拷贝构造函数中 pMatrix 没有分配内存:

Matrix::Matrix(const Matrix &m)
{
    width=m.width;
    length=m.length;

    pMatrix=new int [width*length];  // 加上这句再试试

    if(!m.pMatrix)
    {
        int i,j;
        for(i=0;i<width;i++)
        {
            for(j=0;j<length;j++)
            {
                *(pMatrix+i*width+j)=*(m.pMatrix+i*width+j);
            }
        }
    }
}

#11


搞定了 谢谢各位啦

#12


该回复于2012-04-13 10:51:03被版主删除

#1


该回复于2011-05-03 09:18:47被版主删除

#2




Matrix& Matrix::operator -(const Matrix &m)
{
    if(width!=m.width||length!=m.length)
    {
        cout<<"Error!Can't run the operation."<<endl;
        abort();
    }
    else
    {
        Matrix t(this->width,this->length);    
        int i,j;
        for(i=0;i<width;i++)
        {
            for(j=0;j<length;j++)
            {
                *(t.pMatrix+i*width+j)=*(this->pMatrix+i*width+j)-*(m.pMatrix+i*width+j);
            }
        }
        return t;
    }
}

#3


引用 2 楼 newstudent_never 的回复:
C/C++ code


Matrix&amp; Matrix::operator -(const Matrix &amp;m)
{
    if(width!=m.width||length!=m.length)
    {
        cout<<"Error!Can't run the operation."<<endl;
        abort();
    }
……


又返回的是栈变量, 清栈后调用!!!!!

#4


矩阵乘法会么lz?

会的话应该没问题,加减lz都会了,乘也so easy

#5


Matrix t(this->width,this->length);    
你这句话就多余,  直接用 this指针做 运算, 然后返回 return *this 不好吗??
非得返回  生命周期马上结束的 栈中变量

#6


引用 5 楼 newstudent_never 的回复:
Matrix t(this->width,this->length);  
你这句话就多余, 直接用 this指针做 运算, 然后返回 return *this 不好吗??
非得返回 生命周期马上结束的 栈中变量


运行程序时崩溃的原因就是在这里吗?还有没有什么地方有问题?

#7


关于运算符重载,什么时候重载成成员函数?什么时候重载成友元函数比较好骄傲?可以给个代码例子吗?

#8


你没看到 warning 吗? 返回局部变量的引用了.
改成
Matrix operator +(const Matrix &m);
Matrix operator -(const Matrix &m);

operator+ 这类操作符不应该返回引用.
包括什么时候用成员,什么时候用友元可参考
http://www.adintr.com/myarticle/operator.html

#9


引用 8 楼 adlay 的回复:
你没看到 warning 吗? 返回局部变量的引用了.
改成
Matrix operator +(const Matrix &amp;m);
Matrix operator -(const Matrix &amp;m);

operator+ 这类操作符不应该返回引用.
包括什么时候用成员,什么时候用友元可参考
http://www.adintr.com/myarticle/oper……


我改了,类声明里运算符重载时返回对象,
但是我的程序还是出现问题。
但我试图让主函数在昨晚加法和减法后打印矩阵m3 m4的时候就被系统终止了。
Matrix Matrix::operator +(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<endl;
abort();
}
else
{
Matrix t(width,length);
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(t.pMatrix+i*width+j)=*(m.pMatrix+i*width+j)+*(this->pMatrix+i*width+j);
}
}
return t;
}
}

Matrix Matrix::operator -(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<endl;
abort();
}
else
{
Matrix t(width,length);
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
*(t.pMatrix+i*width+j)=*(this->pMatrix+i*width+j)-*(m.pMatrix+i*width+j);
}
}
return t;
}
}

#10


拷贝构造函数中 pMatrix 没有分配内存:

Matrix::Matrix(const Matrix &m)
{
    width=m.width;
    length=m.length;

    pMatrix=new int [width*length];  // 加上这句再试试

    if(!m.pMatrix)
    {
        int i,j;
        for(i=0;i<width;i++)
        {
            for(j=0;j<length;j++)
            {
                *(pMatrix+i*width+j)=*(m.pMatrix+i*width+j);
            }
        }
    }
}

#11


搞定了 谢谢各位啦

#12


该回复于2012-04-13 10:51:03被版主删除