This is a discussion on Operator Overloading problem within the C++ Programming forums, part of the General Programming Boards category; Code: class Matrix { private: string _name; int _row; int _col; double ** _mat; public: //class methods }; Code: //sets ...

Code:
```class Matrix
{
private:
string _name;
int  _row;
int  _col;
double ** _mat;
public:
//class methods
};```
Code:
```//sets the data of a matrix given a matrix. assignment operator overloading
Matrix & Matrix::operator = (const Matrix & mMatrix)
{
int i,j;
_row = mMatrix._row;
_col = mMatrix._col;
_name = mMatrix._name;
_mat = new double *[_row];
for(i=0;i<_row;i++)
{
_mat[i] = new double [_col];
for(j=0;j<_col;j++)
{
_mat[i][j] = mMatrix._mat[i][j]; //fails here
}
}
return *this;
}```
why does the code above fail?

2. It looks okay. How does it fail?

3. Code:
```// Finds matrix with name n in vector mL; returns -1 if matrix not present
int findMatrix(vector <Matrix> &mL, string n)
{
Matrix temp;
for(vector<Matrix>::iterator iT = mL.begin(); iT != mL.end(); ++iT)
{
temp = *iT;
//if strings match use std function called distance to find the number of elements between
//the beginning of a vector and the current itereator pos
if(temp.getName() == n)
return (int)distance(mL.begin(), iT);
}
return -1;
}```
i call the assignment operator above when setting temp to my iterator thats when it heads into the operator overload and fails

4. Actually, now that I think about it, you should be deleting the previous data in _mat before assigning it new data. But that's not exactly a "failure", just a memory leak.

5. What does your copy constructor look like?

6. Code:
`Unhandled exception at 0x01192af2 in ECE264 Lab 6.exe: 0xC0000005: Access violation reading location 0x00000000.`
thats the error well the only thing i noticed is when i look at iT in debug mode the pointer is pointing to 0x00000000 which makes me believe maybe somehow its getting messed up when i push into the vector ill have to look over my other code

7. There's probably something wrong with your copy constructor, which would be needed to push Matrix's onto the vector.

But why are you using temp in findMatrix? It's an unnecessary copy operation. Just do:
Code:
`if (iT->getName() == n)`

8. Thanks for the heads up but what do you mean by a copy constructor? All i have is a default constructor

9. Originally Posted by omGeeK
Thanks for the heads up but what do you mean by a copy constructor? All i have is a default constructor
You have a copy constructor too, since you did not disable it. Generally, whenever you need to define the destructor, copy constructor or the copy assignment operator, you need to define all three.

Why not use std::vector?

10. Vector for what?

11. Originally Posted by omGeeK
Vector for what?
To store the data. This way, you would not need to define the copy constructor, copy assignment operator or destructor, unless you need to do some other special things in them.

12. Originally Posted by omGeeK
Thanks for the heads up but what do you mean by a copy constructor? All i have is a default constructor
laserlight knows a lot more about this than me, but I believe the way you've currently defined your Matrix you need a copy ctor to provide a "deep copy" of your data.

A copy ctor has a signature like:
Code:
`Matrix(const Matrix& matrix);`
and creates a new Matrix by copying the data from an existing matrix. It would be similar to your operator= method (but has no return statement, of course, and doesn't need to first delete any old data).

If you don't define a copy ctor then you get a default one that does a "shallow copy", simply copying each field, which is not what you want since that would mean that _mat in the copy would point to the same data as the matrix being copied instead of having its own copy of the data.

However, as laserlight said, if you used a vector inside your matrix to hold the data, then you could use the default copy ctor since a vector has a proper copy ctor defined already.

Anyway, if you're still having trouble you should post your whole program.

13. Originally Posted by oogabooga
However, as laserlight said, if you used a vector inside your matrix to hold the data, then you could use the default copy ctor since a vector has a proper copy ctor defined already.
@omGeek: Btw, a vector of int* s would still need a copy constructor, but vector of vector<int> s would not.

14. Originally Posted by manasij7479
@omGeek: Btw, a vector of int* s would still need a copy constructor, but vector of vector<int> s would not.
A vector<vector<int> > also would not.

15. Originally Posted by grumpy
A vector<vector<int> > also would not.
What did you think, I meant by vector of vector<int> `s ?

Page 1 of 3 123 Last