用 operator 重载使得整形矩阵相加 用内部成员函数 有一点问题
希望大神能帮我看一下我的程序错在哪里用operator 重载使得整形矩阵相加 用内部成员函数
#include<iostream>
#include<iomanip>
using namespace std;
int a,b;
class Intmartrix{
public:
Intmartrix();
Intmartrix(int a,int b);
Intmartrix operator+ (Intmartrix &c);
//friend Intmartrix operator+(Intmartrix &c1,Intmartrix &c2);
void dispaly();
~Intmartrix();
private:
int **p;
};
Intmartrix::Intmartrix(){
p=new int*[a];
for(int i=0;i<a;i++)
p[i]=new int[b];
for(i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
p[i][j]=0;
}
}
}
Intmartrix::Intmartrix(int a,int b){
p=new int*[a];
for(int i=0;i<a;i++)
p[i]=new int[b];
for(i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
cout<<"input the data of martrix("<<i+1<<","<<j+1<<")"<<endl;
cin>>p[i][j];
}
}
}
Intmartrix Intmartrix::operator+ (Intmartrix &c){
Intmartrix temp;
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
temp.p[i][j]=p[i][j]+c.p[i][j];
}
}
return temp;
}
void Intmartrix::dispaly(){
cout<<"Now the martrix is:"<<endl;
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
cout<<p[i][j]<<ends;
}
cout<<endl;
}
}
Intmartrix::~Intmartrix(){
for(int i=0;i<a;i++)
delete [] p[i];
delete [] p;
}
//Intmartrix operator+(Intmartrix &c1,Intmartrix &c2){
//for(int i=0;i<a;i++)
//{
//for(int j=0;j<b;j++)
//{
//c1.p[i][j]=c1.p[i][j]+c2.p[i][j];
//}
//}
//return c1;
//}
int main(){
cout<<"input the lenth and line of the martrix"<<endl;
cin>>a>>b;
Intmartrix martrix1(a,b);
Intmartrix martrix2(a,b);
Intmartrix martrix3;
martrix3=martrix1+martrix2;
cout<<"martrix1+martrix2:"<<endl;
martrix3.dispaly();
//operator+(martrix1,martrix2);
//cout<<"martrix1+martrix2:"<<endl;
//martrix1.dispaly();
return 0;
}
本来是要求再要一个友元函数实现的 我在程序中给 / / 掉了,现在成员函数的都调试不出来
应该是在operator +的那个函数出了问题