您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> C/C++ >> C++运算符重载矩阵运算的问题

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

来源:网络整理     时间:2016/7/3 12:16:55     关键词:

关于网友提出的“ C++运算符重载矩阵运算的问题”问题疑问,本网通过在网上对“ C++运算符重载矩阵运算的问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

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

这个程序我写了好久啊。实在没有头绪了。
要求用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 
#include 
#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<>
{
for(j=0;j<>
{
*(pMatrix+i*width+j)=*(m.pMatrix+i*width+j);
}
}
}
}
Matrix::~Matrix()
{
delete [] pMatrix;
pMatrix=NULL;
}
void Matrix::Initialize()
{
for(int i=0;i<>
{
cin>>*(pMatrix+i);
}
}
void Matrix::Display()
{
int i,j;
for(i=0;i<>
{
for(j=0;j<>
{
cout<<*(pMatrix+i*width+j)<<"  ";
}
cout<<>
}
cout<<>
}
Matrix& Matrix::operator +(const Matrix &m)
{
if(width!=m.width||length!=m.length)
{
cout<<"Error!Can't run the operation."<<>
abort();
}
else
{
Matrix t(this->width,this->length);
int i,j;
for(i=0;i<>
{
for(j=0;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."<<>
abort();
}
else
{
Matrix t(this->width,this->length);
int i,j;
for(i=0;i<>
{
for(j=0;j<>
{
*(t.pMatrix+i*width+j)=*(this->pMatrix+i*width+j)-*(m.pMatrix+i*width+j);
}
}
return t;
}
}

主函数
#include 
#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();
}

解决方案1:

引用 2 楼 newstudent_never 的回复:
C/C++ code
Matrix& Matrix::operator -(const Matrix &m)
{
    if(width!=m.width||length!=m.length)
    {
        cout<<"Error!Can't run the operation."<<>
        abort();
    }
……

又返回的是栈变量, 清栈后调用!!!!! 解决方案2:

拷贝构造函数中 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<>
        {
            for(j=0;j<>
            {
                *(pMatrix+i*width+j)=*(m.pMatrix+i*width+j);
            }
        }
    }
}

以上介绍了“ C++运算符重载矩阵运算的问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2241739.html

相关图片

相关文章