I am working on a program that solves a system of equations in matrix form by eliminating Gauss. However, I came across an interesting problem: if my arithmetic operators follow a link, normalizing strings gives incorrect results.
In my implementation, the matrix consists of several vectors, so the operations in the line are just vector arithmetic. Here are the related functions:
Vector:
T& operator[] (const int i);
const T& operator[] (const int i) const;
Vector<T>& operator/=(const T& rhs);
template<class T>
Vector<T>& Vector<T>::operator/=(const T& rhs)
{
if (rhs == 0)
{
throw DivideByZeroException();
}
for (int i = 0; i < _size; ++i)
{
_data[i] /= rhs;
}
return *this;
}
The matrix:
Vector<T>& operator[] (const int i);
const Vector<T>& operator[] (const int i) const;
(Thus, a single [] is used to access a string, and double [] [] is used to access an element.)
Now here is the line that causes the problem:
mat[i] /= mat[i][i];
, - mat[i][i], , operator/= pass by reference.
. ( ) , ? , , , , ?