title: Error while overloading operator + in myVector class
body: <p>I know this question has appeared lots of times here and on the internet, but even by searching on this site I can't overload the <code>+</code> operator in <code>myvec</code> class. The strange thing is that I can overload the <code>=</code> operator, but as I write the declaration and definition of the operator <code>+</code>, I get an error.</p>
  <p>More specifically, I declare the operator as</p>
  <p><code>myvec&amp; myvec::operator+(const myvec&amp; v, const myvec&amp; w)</code></p>
  <p>and the definition is</p>
  <pre class="lang-cpp prettyprint-override"><code>myvec&amp; myvec::operator +(const myvec&amp; v, const myvec&amp; w)
  {
      int d = v.size();
      myvec x(d);
      for (int i = 0; i &lt; d; i++) {
          x(i) = v(i)+w(i);
      }
      return x;
  }
  </code></pre>
  <p>In the following my little class:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  class myvec {
  private:
      int dimension;
      double* data;
  
  public:
      myvec(int dim);
      myvec(const myvec&amp; v);
      ~myvec();
  
      int size() const;
      void Print();
      double&amp; operator ()(int i);
      myvec&amp; operator =(const myvec&amp; v);
      myvec&amp; operator +(const myvec&amp; v, const myvec&amp; w);
  };
  
  
  myvec::myvec(int dim)
  {
      dimension = dim;
      data = new double[dim];
  
      for (int i = 0; i &lt; dimension; i++) {
          data[i] = 0.0;
      }
  }
  
  myvec::myvec(const myvec&amp; v)
  {
      int dimensione = v.size();
      data = new double[dimensione];
      for (int i = 0; i &lt; dimensione; i++) {
          data[i] = v.data[i];
      }
  }
  
  myvec::~myvec()
  {
      dimension = 0;
      delete[] data;
      data = NULL;
  }
  
  int myvec::size() const
  {
      return dimension;
  }
  
  double&amp; myvec::operator ()(int i)
  {
      return data[i];
  }
  
  myvec&amp; myvec::operator =(const myvec&amp; v)
  {
  
      int dim  = v.size();
      for (int i = 0; i &lt; dim; ++i) {
          data[i] = v.data[i];
      }
      return *this;
  }
  
  myvec&amp; myvec::operator +(const myvec&amp; v, const myvec&amp; w)
  {
      int d = v.size();
      myvec x(d);
      for (int i = 0; i &lt; d; i++) {
          x(i) = v(i)+w(i);
      }
      return x;
  }
  
  void myvec::Print()
  {
      for (int i = 0; i &lt; dimension; i++) {
          cout &lt;&lt; data[i]&lt;&lt;endl;
      }
  }
  </code></pre>
  <p>The compiler gives me the error:</p>
  <pre><code>testmyvec.cpp.cc:77:59: error: ‘myvec&amp; myvec::operator+(const myvec&amp;, const myvec&amp;)’ must take either zero or one argument
  </code></pre>
  <p>It's clearly referring to the definition of the <code>+</code> operator. How can I fix it in order to overload the operator?</p>