### problem in operators...

hi:)
this is my code about matrixes
before writing the operator ^ it worked correctly
but now not!
i don't know where is the problem...

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

/************************* Matrix Class ************************/
class Matrix {
//friend list:
friend istream& operator>>(istream& in, Matrix& m);
friend ostream& operator<<(ostream& in, Matrix& m);

int** a; //2D array pointer
int R, C; //num of rows and columns

public:
Matrix();
Matrix(const Matrix&);
~Matrix();

Matrix operator ^ (int n){
s6 ++;
Matrix temp1;
temp1.a = new int *[R];
for(int i=0; i<R; i++)
temp1.a[i] = new int[C];

for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
temp1.a[i][j] = a[i][j];

Matrix temp2;
temp2.a = new int* [R];
for(int i=0; i<R; i++)
temp2.a[i] = new int[C];

for(int l=0; l<n-1 ;l++){
for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
{
int k=0;
for(int m=0; m<R; m++)
k += (temp1.a[i][m] * a[m][j]);

temp2.a[i][j] = k;

}

for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
temp1.a[i][j] = temp2.a[i][j];
}
return temp1;
}

Matrix operator+ (const Matrix &M){
s4++;
Matrix temp;
temp.R = R;
temp.C = C; temp.a = new int*[R];
for(int i=0; i<R; i++)
temp.a[i] = new int[C];

for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
temp.a[i][j] = a[i][j] + M.a[i][j];

return temp;
}

Matrix& operator = (const Matrix& M){
s5++;
if(a != NULL)
{
for(int i=0; i<R; i++)
delete [] a[i];
delete a;
a = NULL;
R = 0;
C = 0;
}
R = M.R;
C = M.C;
a = new int*[R];
for(int i=0; i<R; i++)
a[i] = new int[C];

for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
a[i][j] = M.a[i][j];

return *this;
}
};

Matrix::Matrix() {
a = NULL; //initializing the matrix to be 0*0
R = 0; // "
C = 0; // "
s1++;
}

Matrix::Matrix(const Matrix& M){
if(a != NULL)
{
for(int i=0; i<R; i++)
delete [] a[i];
delete a;
a = NULL;
R = 0;
C = 0;
}
R = M.R;
C = M.C;
a = new int*[R];
for(int i=0; i<R; i++)
a[i] = new int[C];

for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
a[i][j] = M.a[i][j];

s2++;

}
Matrix::~Matrix() {
if(a != NULL) {
for(int i = 0; i < R; i++) {
delete [] a[i];
}
delete a;
a = NULL;
}
s3++;
}

/************************* Operators ************************/

istream& operator>>(istream& in, Matrix& m) {
m.~Matrix(); //maybe m has data
in >> m.R >> m.C; //reading rows & columns from input

m.a = new int*[m.R]; //take memory for matrix
for(int i = 0; i < m.R; i++)
m.a[i] = new int[m.C];

for(int i = 0; i < m.R; i++) //reading the matrix data
for(int j = 0; j < m.C; j++)
in >> m.a[i][j];
return in;
}

ostream& operator<<(ostream& out, Matrix& m) {
out << m.R << " " << m.C << endl; //writing rows & columns to output

for(int i = 0; i < m.R; i++) { //writing the matrix data
for(int j = 0; j < m.C; j++)
out << m.a[i][j] << " ";
out << endl;
}
return out;
}

/************************* main function ************************/
int main() {
ifstream in("in.txt");
ofstream out("out.txt");

Matrix x, y;
in >> x >> y;
out << y;

Matrix z(x);
out << z;
z.~Matrix();

z = y;
out << z;
z.~Matrix();

z = x + y;
out << z;

Matrix w;
in >> w;
z = w ^ 4;
out << z;

return 0;
}
Please use [code]code tags[/code].

I don't know what's wrong with your ^ operator... but I see you explicitly calling the dtor a lot...

`z.~Matrix();`

DO NOT DO THIS. The dtor is called automatically when the object goes out of scope. That's the whole point of it. You must not call it manually. The only reason to do this would be if you are using placement new (read: you're not, so don't worry about it. Just know that you should not do this).

This could be causing all manner of weird problems in your program so try fixing that first and see if your problem goes away.

Also... this screams "operator abuse" to me. ^ Is the XOR operator... I don't see how XORing a matrix would be useful. And it doesn't look like that's what the operator is doing anyway. You probably should not be overloading the ^ operator here.

Don't get cute with operator overloads. The whole point is for them to make code more intuitive. When you start inventing new purposes for the operators they are no longer intuitive.
i removed them
but no diffrence...
Topic archived. No new replies allowed.