I have many errors in the following code.Could you suggest a better way to overload '+' in this class.My way doesn't work at all.The other functions are nicely doing what they are meant to do.
class matrix;
int matrix::*v = &matrix.value;
class matrix
{
int x;
int y;
int * value;
void allocate()
{
cout<<"Input the order of the matrix:\n";
cin>>x>>y;
value = newint[x*y];
order[0]=x;
order[1]=y;
}
public:
int order[2];
void set_values()
{
int z(0);
allocate();
cout<<"Now put the values:\n>";
for(z=0;z<(x*y);z++)
{
cin>>*(value+z);
cout<<">";
}
}
void out_value()
{
int m(0),z(0),count(0);
cout<<"The value are..\n";
for(z=0;z<x;z++)
{
for(m=0;m<y;m++)
{
cout<<*(value+count)<<"\t";
count++;
}
cout<<endl;
}
}
matrix operator+(matrix a)
{
matrix b;int z;
if (order[0]==a.order[0]&&order[1]==a.order[1])
{
for(z=0;z<(x*y);z++)
{
b->*v = a->*v + *v;
}
return b;
}
else {cout<<"Not conformable for addition!\n";}
}
};
In line 58 , I want to add all the data in *value of the argument object to the current matrix. What has allocate() got to do with it? I am pretty novice at using pointers, could you write the example code for line 58?
Each instance of matrix has a "value" member which is a pointer to an array of x*y elements.
You need to allocate that memory inside the "b" instance in operator+. Your allocate() method
does that (and unfortunately it also asks the user for input).
Once you do that, line 58 becomes b.value[ z ] = a.value[ z ] + value[ z ];
class matrix
{
public:
matrix(int,int);
matrix(){x=0;y=0;};
int x;
int y;
int order[2];
int * value;
void allocate();
void allocate(int,int);
// public:
void set_values();
void out_value();
//int get_i(int z);
//void set_i(int v,int z);
matrix operator+(matrix a);//Whatever I try, this does not
//seem to work
};
void matrix::allocate(int a,int b)
{
value = newint[a*b];
order[0]=a;
order[1]=b;
x=a;
y=b;
}
void matrix::allocate()
{
cout<<"Input the order of the matrix:\n";
cin>>x>>y;
value = newint[x*y];
order[0]=x;
order[1]=y;
}
matrix matrix::operator+(matrix a)
{
matrix c;
int z;
if (order[0]==a.order[0] && order[1]==a.order[1])
{
for(z=0;z<(x*y);z++)
{
c.allocate(x,y);
c.value[z] = a.value[z] + value[z];
}
return c;
}
else {cout<<"Not conformable for addition!\n";}
}