title: C++ My first class operator overload, with header
body: <p>im new to C++, although i do know the general C syntax. I've been trying to create a class with operator overloading. But i can't get it to work. Well partially got it to work.</p>
  
  <p>Working operator overloading in same source:</p>
  
  <pre><code>//test.cpp
  #include &lt;iostream&gt;
  
  class Fraction
  {
      int gcd(int a, int b) {return b==0 ? a : gcd(b,a%b); }
      int n, d;
  public:
      Fraction(int n, int d = 1) : n(n/gcd(n,d)), d(d/gcd(n,d)) {}
      int num() const { return n; }
      int den() const { return d; }
      Fraction&amp; operator*=(const Fraction&amp; rhs) {
          int new_n = n*rhs.n / gcd(n*rhs.n, d*rhs.d);
          d = d*rhs.d / gcd(n*rhs.n, d*rhs.d);
          n = new_n;
          return *this;
      }
  };
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const Fraction&amp; f){
     return out &lt;&lt; f.num() &lt;&lt; '/' &lt;&lt; f.den() ;
  }
  bool operator==(const Fraction&amp; lhs, const Fraction&amp; rhs) {
      return lhs.num() == rhs.num() &amp;&amp; lhs.den() == rhs.den();
  }
  bool operator!=(const Fraction&amp; lhs, const Fraction&amp; rhs) {
      return !(lhs == rhs);
  }
  Fraction operator*(Fraction lhs, const Fraction&amp; rhs)
  {
      return lhs *= rhs;
  }
  
  int main()
  {
     Fraction f1(3,8), f2(1,2), f3(10,2);
     std::cout &lt;&lt; f1 &lt;&lt; '*' &lt;&lt; f2 &lt;&lt; '=' &lt;&lt; f1*f2 &lt;&lt; '\n'
               &lt;&lt; f2 &lt;&lt; '*' &lt;&lt; f3 &lt;&lt; '=' &lt;&lt; f2*f3 &lt;&lt; '\n'
               &lt;&lt; 2  &lt;&lt; '*' &lt;&lt; f1 &lt;&lt; '=' &lt;&lt; 2 *f1 &lt;&lt; '\n';
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>3/8*1/2=3/16
  1/2*5/1=5/2
  2*3/8=3/4
  </code></pre>
  
  <p>Source: <a href="http://en.cppreference.com/w/cpp/language/operators" rel="nofollow">http://en.cppreference.com/w/cpp/language/operators</a></p>
  
  <p>Now my code, trying to apply the code from above</p>
  
  <pre><code>//vectors.h
  class Vector2
  {
      public:
      Vector2(void);
      ~Vector2(void);
      int counter;
      Vector2&amp; operator+=(const Vector2&amp; vec);
  }
  
  //vectors.cpp
  #include "vectors.h"
  
  Vector2::Vector2(void)
  {
      counter = 0;
  }
  Vector2::~Vector2(void)
  {
  }
  
  Vector2&amp; operator+=(Vector2&amp; vec)//error: too few parameters
  {
      int new_n = counter + vec.counter;
      counter = new_n;
      return *this;//error: this may only be used in a non-static member function.
  }
  
  //main.cpp
  #include &lt;stdio.h&gt;
  #include "vectors.h"
  
  int main(void)
  {
      Vector2 vector();
      while(true)
      {
          vector += vector;//error: expression must be a modifiable value
          printf("Vector counter: %d\n",vector.counter);
      }
  }
  </code></pre>
  
  <p>What i'm trying to do:</p>
  
  <p>I'm trying to make my own class, and use operator overloading. But the part i can't get to work is defining the class with a header while keeping operator overloading working.</p>
  
  <p>Thanks for reading my question</p>