title: error: declaration of ‘operator*’ as non-function
body: <p>I have some problems with bound template friend functions of template classes.
  I've refered the page which has the similar and simple problem as me: <a href="https://stackoverflow.com/questions/61672608/vect-hpp1333-error-declaration-of-operator-as-non-function">vect.hpp:13:33: error: declaration of ‘operator&lt;&lt;’ as non-function
  </a>, but I still confused, the problem cannot be solved. Here are my codes:</p>
  <pre><code>#include&lt;iostream&gt;  
  #include&lt;valarray&gt;
  
  using namespace std;
  
  //forward declaration
  template&lt;typename ElemType, size_t row, size_t col = row&gt;
  class matrix;
  
  template&lt;typename ET, size_t R, size_t C&gt;
  matrix&lt;ET, R, C&gt; operator*(ET multier, matrix&lt;ET, R, C&gt; mattem);
  
  template&lt;typename ElemType, size_t row, size_t col&gt;
  class matrix
  {
      private:
          valarray&lt;ElemType&gt; mat;
      public:
          matrix(): mat(row*col) {}
          matrix(initializer_list&lt;ElemType&gt;);
  
          matrix operator*(ElemType multier) const
          {
              matrix mattem;
              mattem.mat = this-&gt;mat*multier;
              return mattem;
          }
          
          //friend function prototype
          friend matrix&lt;ElemType, row, col&gt; operator*&lt;&gt;(ElemType multier, matrix&lt;ElemType, row, col&gt; mattem);
  
          void show() const
          {
              for (ElemType Elem : this-&gt;mat)
                  cout &lt;&lt; Elem &lt;&lt; &quot; &quot;;
              cout &lt;&lt; endl;
          }
  };
  
  template&lt;typename ElemType, size_t row, size_t col&gt;
  matrix&lt;ElemType, row, col&gt;::matrix(initializer_list&lt;ElemType&gt; ini)
  {
      try
      {
          if (ini.size() != row*col)
              throw &quot;Inappropriate length!&quot;;
          this-&gt;mat = ini;
      }
      catch (const char * str)
      {
          cout &lt;&lt; str &lt;&lt; endl;
      }
  }
  
  //friend function definition
  template&lt;typename ElemType, size_t row, size_t col&gt;
  matrix&lt;ElemType, row, col&gt; operator*&lt;&gt;(ElemType multier, matrix&lt;ElemType, row, col&gt; mattem)
  {
      return mattem*multier;
  }
  
  int main(void)
  {
      matrix&lt;int, 2, 3&gt; test = {1, 3, 5, 8, 9, 7};
      matrix&lt;int, 2, 3&gt; abc =2*test;
      abc.show();
  }
  </code></pre>
  <p>I compiled and run it in <code>WSL2 (Ubuntu 9.3.0-17ubuntu1~20.04)</code> using <code>gcc version 9.3.0</code>, and I got a series errors as below:</p>
  <pre><code>test.cpp:28:51: error: declaration of ‘operator*’ as non-function
     28 |         friend matrix&lt;ElemType, row, col&gt; operator*&lt;&gt;(ElemType multier, matrix&lt;ElemType, row, col&gt; mattem);
        |                                                   ^
  test.cpp:28:51: error: expected ‘;’ at end of member declaration
     28 |         friend matrix&lt;ElemType, row, col&gt; operator*&lt;&gt;(ElemType multier, matrix&lt;ElemType, row, col&gt; mattem);
        |                                                   ^
        |                                                    ;
  test.cpp:28:52: error: expected unqualified-id before ‘&lt;’ token
     28 |         friend matrix&lt;ElemType, row, col&gt; operator*&lt;&gt;(ElemType multier, matrix&lt;ElemType, row, col&gt; mattem);
        |                                                    ^
  test.cpp:54:91: error: template-id ‘operator*&lt;&gt;’ in declaration of primary template
     54 | matrix&lt;ElemType, row, col&gt; operator*&lt;&gt;(ElemType multier, matrix&lt;ElemType, row, col&gt; mattem)
        |                                                                                           ^
  test.cpp: In function ‘int main()’:
  test.cpp:62:29: error: conversion from ‘matrix&lt;[...],[...],2&gt;’ to non-scalar type ‘matrix&lt;[...],[...],3&gt;’ requested
     62 |     matrix&lt;int, 2, 3&gt; abc =2*test;
        |                            ~^~~~~
  </code></pre>