-2

As above,but I define matrix dimension by template parameter.When I try to implement a matrix multiplication, some trouble occurs.My matrix multiplication prototype as bellow:

matrix_array<class __item__, std::size_t ROW, std::size_t COL>;
matrix_array<__item__, ROW, COL> operator* (
        const matrix_array<__item__, COL, >& b) const;

So there is a question, how to pass the third template argument of matrix b?I can just to define a new template parameter, but it's so terrible.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Shylock Hg
  • 23
  • 4
  • 1
    Names such as `__item__` are not allowed to be created in user-written code. –  Oct 27 '18 at 14:47
  • This template takes three parameters. End of story. It is your job to figure out what each template parameter must be. Nobody here can figure it out for you, especially based on almost nothing of useful information that's shown in the question. Your shown code fails to meet the requirements for a [mcve] as explained in stackoverflow.com's [help], and because of that nobody will have any idea what "some trouble" even is, much less what can be done to fix it. – Sam Varshavchik Oct 27 '18 at 14:55
  • bad choice an `operator*` as class method; particularly for matrix multiplication – max66 Oct 27 '18 at 16:36
  • @max66 It seems works.What is the problem? – Shylock Hg Oct 29 '18 at 03:59
  • It's a long story. I suggest you to follows the rules in [this answer](https://stackoverflow.com/questions/4421706/operator-overloading-in-c/4421729#4421729). I just point your attention to one point: suppose you have `a * b` where `a` is a matrix and `b` is an object that can be converted to a matrix; the operation goes well in both cases (`operator*()` as class method or external function); but when `b` is a matrix and `a` is an object that can be converted to a matrix, the operation goes well only if `operator*()` is a external function. This asymmetry (in the method case) is a bad choice. – max66 Oct 29 '18 at 09:30

1 Answers1

0

In matrix multiplication, one "size" should be common between the 2 matrices, so some naming choice might be confusing.

With non-member function, it might be simpler to understand, your expected signature would be:

template <typename T, std::size_t ROW, size_t K, size_t COLUMN>
Matrix<T, ROW, COLUMN> operator * (const Matrix<T, ROW, K>& lhs,
                                   const Matrix<T, K, COLUMN>& rhs);

So as member, lhs would be *this:

template <typename T, std::size_t ROW, size_t COL /*K*/>
class Matrix
{
    // ...

    template <size_t COLUMN>
    Matrix<T, ROW, COLUMN> operator * (const Matrix<T, COL, COLUMN>& rhs) const;
    // "confusing" name: rhs has 2 "column" names
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302