Ive created class mat (matrix) with consructor functions and operator + overloaded to add two matrix' but when i compile and run my code, Everything works fine till i initialize my m1, m2..and then result is "segmentation fault (core dumped)". I cant figure out why is it showing it? i've not posted here the ostream& operator << function to shorten the code length.
class mat{
int r,c;
float **p;
public:
mat(){}
mat(int,int);
mat(int,int,float);
void initialize();
mat operator+(mat); //defined
friend ostream& operator<<(ostream&,mat&);
};
void mat :: initialize(void){
int i,j;
cout<<"\nEnter the elements : ";
for(i=0;i<r;++i){
for(j=0;j<c;++j){
cin>>p[i][j];
}
}
return;
}
mat mat :: operator+(mat x){
mat tmp;
int i,j;
for(i=0;i<r;++i){
for(j=0;j<c;++j){
tmp.p[i][j]=(p[i][j])+(x.p[i][j]);
}
}
return tmp;
}
mat :: mat (int a, int b){
r=a;
c=b;
p=new float*[r];
for(int i=0; i<r; ++i){
p[i]=new float[c];
}
}
mat :: mat (int a, int b, float t){
r=a;
c=b;
p=new float*[r];
for(int i=0; i<r; ++i){
p[i]=new float[c];
}
for(int i=0;i<r;++i){
for(int j=0;j<c;++j){
p[i][j]=t;
}
}
}
int main(){
mat m1(3,3),m2(3,3),m3(3,3,0);
cout<<"\nInitialize M1";
m1.initialize();
cout<<"\nInitialize M2";
m2.initialize();
m3=m1+m2;
cout<<m3;
return(0);
}
i'm new to coding.Help me out in easy language please.