0

I have been trying to get an operator* between two of my classes. I am building a small geometric library for my game and I have two class templates: Vector<typename T, size_t s> and Matrix<typename T, size_t rows, size_t columns>

I would like to Multiply the Matrix by a Vector so I have overloaded an operator* in the Matrix class it looks like this.

 Vector<T,c> operator*(Vector<T, c>& a)
    {
      Vector<T,c> t = {{0}};
      for(int y=0; y < r; y++)
    for(int x = 0;x < c;x++)
      t.data[x] += this->data[y*c+x] * a[x];
      return t;
    }

This works when I place the matrix first and vector second such as Mat_A * Vec_A but when I reverse them such as Vec_A * Mat_A the operator* in Vector is called and because the vector class doesn't have a way of determining how many rows has the matrix have I cannot implement the operator.

Is there a way around this without actually reimplementing the whole design?

Thank you

Strong will
  • 556
  • 4
  • 9
  • 3
    Rather than having `operator*` a member of `Matrix`, make it a standalone nonmember function taking two parameters. Provide two overloads, taking parameters in different order (and perhaps the third for multiplying two matrices). – Igor Tandetnik Jul 20 '17 at 20:58
  • Oh good idea, thank you. – Strong will Jul 20 '17 at 21:14
  • I am bit confused if the operator operates on templates shouldn't he be a template too and who is going to pass the template parameters ? – Strong will Jul 20 '17 at 21:29
  • Yes it should be a function template. Template parameters can usually be deduced from function arguments. – Igor Tandetnik Jul 20 '17 at 21:41

0 Answers0