I've defined a matrix class in C++, with a single data_[] array, and an overloaded () operator to access it. It worked fine as a container, but I defined an addition operation:
Code:
matrix matrix::operator +(const matrix op1)
{
if((op1.rows_==rows_)&&(op1.cols_==cols_))
{
matrix result(rows_,cols_);
for(int r=0;r<rows_;r++)
{
for(int c=0;c<cols_;c++)
{
result(r,c)=op1(r,c)+data_[(cols_*r)+c]; // fill result matrix with sums
};
};
return result;
}else{
cout << "matricies are not of equal dimension" << endl;
matrix ret(1,1);
return ret;
};
};
Now, I added two matrices of equal dimension, and it went into the first branch of the conditional. It went fine, I followed it step by step, it filled result with the sums. It worked fine when they weren't equal as well. But once it's passed "return result;", and exits the function, after destroying the internal variables, it steps back to the addition expression, and calls the destructor:
Code:
matrix::~matrix()
{
delete[] data_;
};
And the delete[] causes an access violation. I can't figure out what it's trying to delete. Any ideas?