0

I'm doing a course project and I created a matrix class with 2d vector in cpp. I'm trying to override * operator to an global operator with the matrix obj.

this is my declaration:

friend Matrix<T> operator * (T t, const Matrix<T> &m);

and this is the function:

template <typename T>
Matrix<T> operator * (T t, const Matrix<T> &m)
{
    int i, j;
    Matrix<T> ans(rows, cols);
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < rows; j++)
        {
            ans[i][j] = t * m.mat[i][j];
        }
    }
return ans;
}

my error is: error C2244: 'Matrix::operator *' : unable to match function definition to an existing declaration

what is wrong with my code??

Gabriel
  • 121
  • 1
  • 1
  • 8
  • 2
    That's because `Matrix::operator * (...)` is a *member* function and not a stand-alone function. You need to define `template Matrix operator * (T t, const Matrix &m)`. Also, your declaration needs to be a template as well. – Some programmer dude Jan 17 '15 at 16:15

1 Answers1

1

The friend function you declared, though in class, is not a member function.
Adjust the definition like this:

template <typename T>
Matrix<T> operator * (T t, const Matrix<T> &m)
{
    // […]
}
Columbo
  • 60,038
  • 8
  • 155
  • 203