title: Operator with different types
body: <p>I am quite new to C++ and I am implementing a, quite simple, (linear algebra) vector class as a template.</p>
  <p>Now, I am trying to implement the addition and subtraction operators but with the ability to perform the operations not only between vectors, but also with scalars (short, long, float, double, etc.) (this will be an element-wise operation).</p>
  <p>The relevant part of the code can be seen below and both declaration and definition (implementation) of the template class is in the same <code>.hpp</code> file (checked <a href="https://stackoverflow.com/questions/50673352/c-xcode-ld-symbols-not-found-for-architecture-x86-64-clang-error-linker-c">this</a> question and didn't seem to provide a solution for this exact reason).</p>
  <pre><code>// Forward declaration of friend function
  template&lt;class S, class T&gt;
  Vector&lt;T&gt; operator +(const S&amp; scalar, const Vector&lt;T&gt;&amp; vec);
  
  template&lt;class T&gt;
  class Vector {
    public:
      Vector(unsigned long length);
      Vector(Vector&lt;T&gt; vec);
  
      template&lt;class S&gt;
      friend Vector&lt;T&gt; operator +(const S&amp; scalar, const Vector&lt;T&gt;&amp; vec);
      
      template&lt;class S&gt;
      Vector&lt;T&gt; operator +(const S&amp; scalar) const;
  
      template&lt;class S&gt;
      Vector&lt;T&gt;&amp; operator +=(const S&amp; scalar);
  
    private:
      unsigned long mLen;
      T* mData;
  };
  
  // Constructor
  Vector&lt;T&gt;::Vector(const unsigned long length) : mLen(length) {
    mData = new T[mData];
  
    for(unsigned long i = 0; i &lt; mLen; ++i) {
      mData[i] = T();
    }
  }
  
  // Copy constructor
  template&lt;class T&gt;
  Vector&lt;T&gt;::Vector(Vector&lt;T&gt; vec) {
    mLen = 0;
    mData = nullptr;
    std::swap(mData, vec.mData);
  }
  
  // Implementation of the operators
  template&lt;class S, class T&gt;
  Vector&lt;T&gt; operator +(const S&amp; scalar, const Vector&lt;T&gt;&amp; vec) {
    return vec + scalar;
  }
  
  template&lt;class T&gt; template&lt;class S&gt;
  Vector&lt;T&gt; operator +(const S&amp; scalar) const {
    Vector&lt;T&gt; result(*this);
    return result += scalar;
  }
  
  template&lt;class T&gt; template&lt;class S&gt;
  Vector&lt;T&gt;&amp; Vector&lt;T&gt;::operator +(const S&amp; scalar) {
    // mLen is the length of the object (member variable)
    for(unsigned long i = 0; i &lt; mLen; ++i) {
      mData[i] += scalar; // mData is the array holding the values (member variable)
    }
  
    return *this;
  }
  </code></pre>
  <p>The addition between a Vector and a scalar is implemented (as a member function though) and works as intended (result-wise). Furthermore, I can guarantee (for the moment at least) that <code>S</code> will always be one of <code>short</code>, <code>int</code>, <code>float</code> or <code>double</code>.</p>
  <p>So, when in my tests I try to do something like</p>
  <pre><code>Vector&lt;int&gt; vec(10);
  
  1 + vec;
  </code></pre>
  <p>I get the following error</p>
  <blockquote>
  <p>Undefined symbols for architecture x86_64:
  &quot;operator+(int const&amp;, Vector const&amp;)&quot;, referenced from:
  _main in main.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)</p>
  </blockquote>
  <p>I am working on XCode 11.3.1 for this project.</p>
  <p>Any insights would be most welcome. Additionally, I know that my understanding of templates (and a whole lot more about C++) is still very limited, so if you would require more information to get a better idea please let me know.</p>
  <p>Thanks in advance.</p>