V
valerij
Yes, hi
How to write "operator +" and "operator =" functions in a class with
a defined constructor? The following code demonstrates that I don't
really understand how to do it... I think it has something to do with
the compiler calling the destructor twice. Could someone point out
where I go wrong?
P.S.: The error it gives is "Debug Assertion Failure ....." (at run
time)
P.P.S: Everything else works just fine (without the use of "operator
+"
and "operator =" functions)
//code "tested" on Microsoft Visual C++ 6.0
#include <iostream>
using namespace std;
class DatArray {
public:
int rows, columns;
double **a;
DatArray(int r, int c);
~DatArray();
void operator = (double d1);
void operator = (DatArray da1); //problematic
void operator ++ ();
//DatArray operator + (double d1); //does not work
};
DatArray:atArray(int r, int c) {
int c1;
rows = r;
columns = c;
a = new double* [rows];
for (c1 = 0; c1 < rows; c1++) a[c1] = new double [columns];
}
DatArray::~DatArray() {
int c1;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;
}
void DatArray:perator = (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = d1;
}
void DatArray:perator = (DatArray da1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = da1.a[c1][c2];
}
void DatArray:perator ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
}
/*DatArray DatArray:perator + (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = a[c1][c2] + d1;
return *this;
}*/
int main() {
int c1, c2;
DatArray myarray(3, 4), myarray2(3, 4);
myarray = 10.1;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray.a[c1][c2]
<< " ";
cout << endl;
}
cout << endl;
for (c1 = 0; c1 < 100; c1++) myarray++;
//myarray2 = myarray + 100; //this should be simpler, but does not
work ?!?!?!?!?!
myarray2 = myarray;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray2.a[c1][c2]
<< " ";
cout << endl;
}
cin.get(); //currently, after this, ERROR ... WHY?!?!?!?!?! (how to
fix this?!?!?!?!?)
return 0;
}
//end of code "tested" on Microsoft Visual C++ 6.0
Thanks,
Valerij
How to write "operator +" and "operator =" functions in a class with
a defined constructor? The following code demonstrates that I don't
really understand how to do it... I think it has something to do with
the compiler calling the destructor twice. Could someone point out
where I go wrong?
P.S.: The error it gives is "Debug Assertion Failure ....." (at run
time)
P.P.S: Everything else works just fine (without the use of "operator
+"
and "operator =" functions)
//code "tested" on Microsoft Visual C++ 6.0
#include <iostream>
using namespace std;
class DatArray {
public:
int rows, columns;
double **a;
DatArray(int r, int c);
~DatArray();
void operator = (double d1);
void operator = (DatArray da1); //problematic
void operator ++ ();
//DatArray operator + (double d1); //does not work
};
DatArray:atArray(int r, int c) {
int c1;
rows = r;
columns = c;
a = new double* [rows];
for (c1 = 0; c1 < rows; c1++) a[c1] = new double [columns];
}
DatArray::~DatArray() {
int c1;
for (c1 = 0; c1 < rows; c1++) delete [] a[c1];
delete [] a;
}
void DatArray:perator = (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = d1;
}
void DatArray:perator = (DatArray da1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = da1.a[c1][c2];
}
void DatArray:perator ++ () {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2]++;
}
/*DatArray DatArray:perator + (double d1) {
int c1, c2;
for (c1 = 0; c1 < rows; c1++)
for (c2 = 0; c2 < columns; c2++) a[c1][c2] = a[c1][c2] + d1;
return *this;
}*/
int main() {
int c1, c2;
DatArray myarray(3, 4), myarray2(3, 4);
myarray = 10.1;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray.a[c1][c2]
<< " ";
cout << endl;
}
cout << endl;
for (c1 = 0; c1 < 100; c1++) myarray++;
//myarray2 = myarray + 100; //this should be simpler, but does not
work ?!?!?!?!?!
myarray2 = myarray;
for (c1 = 0; c1 < myarray.rows; c1++) {
for (c2 = 0; c2 < myarray.columns; c2++) cout << myarray2.a[c1][c2]
<< " ";
cout << endl;
}
cin.get(); //currently, after this, ERROR ... WHY?!?!?!?!?! (how to
fix this?!?!?!?!?)
return 0;
}
//end of code "tested" on Microsoft Visual C++ 6.0
Thanks,
Valerij