ok, i found some similar posts on stackoverflow, but I couldn't find any that pertained to my exact situation and I was confused with some of the answers given. Ok, so here is my problem:
I have a template matrix class as follows:
template <typename T, size_t ROWS, size_t COLS>
class Matrix
{
public:
template<typename, size_t, size_t>
friend class Matrix;
Matrix( T init = T() )
: _matrix(ROWS, vector<T>(COLS, init))
{
/*for( int i = 0; i < ROWS; i++ )
{
_matrix[i] = new vector<T>( COLS, init );
}*/
}
Matrix<T, ROWS, COLS> & operator+=( const T & value )
{
for( vector<T>::size_type i = 0; i < this->_matrix.size(); i++ )
{
for( vector<T>::size_type j = 0; j < this->_matrix[i].size(); j++ )
{
this->_matrix[i][j] += value;
}
}
return *this;
}
private:
vector< vector<T> > _matrix;
};
and I have the following global function template:
template<typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator+( const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs )
{
Matrix<T, ROWS, COLS> returnValue = lhs;
return returnValue += lhs;
}
To me, this seems to be right. However, when I try to compile the code, I get the following error (thrown from the operator+ function):
binary '+=' : no operator found which takes a right-hand operand of type 'const matrix::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion)
I can't figure out what to make of this. Any help if greatly appreciated!