title: "undefined reference to" operator overloading in C++
body: <p>I want to create class Q(rational numbers), which have the following functions:</p>
  
  <p>1)print(): printing rational number in form :numerator/denominator</p>
  
  <p>2)cancel(): performs cancelation, if it's possible: (3/9 = 1/3)</p>
  
  <p>3)value(): value of the rational number as double type;</p>
  
  <p>4)Operator overloading with addition, subtraction, division, multiplication.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;stdexcept&gt;
  using namespace std;
  
  
  class Q{
  private:
      int num;
      int denom;
  
  public:
      Q();
      Q(int num,int denom);
      void print();
      Q cancel();
      void value();
      friend Q operator*(Q lhs,Q rhs);
      friend Q operator/(Q lhs,Q rhs);
      friend Q operator+(Q lhs,Q rhs);
      friend Q operator-(Q lhs,Q rhs);
  };
  
  Q::Q(int numerator,int denominator){
      num = numerator;
      denom = denominator;
  }
  
  
  void Q::print(){
      cout &lt;&lt; to_string(num) + "/" + to_string(denom);
  }
  
  Q Q::cancel(){
      vector&lt;int&gt; canc_val = {2,3,5,7};
      while(true){
          if(num % canc_val[0] == 0 &amp;&amp; denom % canc_val[0] == 0){
              num /= canc_val[0];
              denom /= canc_val[0];
          } else if(num % canc_val[1] == 0 &amp;&amp; denom % canc_val[1] == 0){
              num /= canc_val[1];
              denom /= canc_val[1];
          } else if(num % canc_val[2] == 0 &amp;&amp; denom % canc_val[2] == 0){
              num /= canc_val[2];
              denom /= canc_val[2];
          }else if(num % canc_val[3] == 0 &amp;&amp; denom % canc_val[3] == 0){
              num /= canc_val[3];
              denom /= canc_val[3];
          }else{
              break;
          }
      }
      return Q(num,denom);
  }
  
  void Q::value(){
      double res;
      res = num/denom;
      cout &lt;&lt; res;
  }
  
  Q operator*(Q lhs,Q rhs){
      Q temp;
      temp.num = lhs.num * rhs.num;
      temp.denom = lhs.denom * rhs.denom;
      return temp;
  }
  
  Q operator/(Q lhs,Q rhs){
      Q temp;
      temp.num = lhs.num * rhs.denom;
      temp.denom = lhs.denom * rhs.num;
      return temp;
  }
  
  Q operator+(Q lhs,Q rhs){
      Q temp;
      temp.num = lhs.num * rhs.denom + rhs.num*lhs.denom;
      temp.denom = lhs.denom * rhs.denom;
      return temp;
  }
  
  Q operator-(Q lhs,Q rhs){
      Q temp;
      temp.num = lhs.num * rhs.denom - rhs.num*lhs.denom;
      temp.denom = lhs.denom * rhs.denom;
      return temp;
  }
  
  int main(){
      Q number_1(3,7);
      Q number_2(3,9);
      Q number_3(15,125);
  
      number_1.print();
      cout &lt;&lt; "\n";
      number_2.print();
      number_2.cancel();
      cout &lt;&lt; "\n";
      number_2.print();
      cout &lt;&lt; "\n";
      number_3.value();
      cout &lt;&lt; "\n";
      number_3.cancel();
      cout &lt;&lt; "\n";
      number_3.print();
  
      Q number_4 = number_1 + number_2;
      number_4.print();
      number_4.cancel();
      cout &lt;&lt; "\n";
      number_4.print();
  
      Q number_5 = number_2 - number_3;
      number_5.print();
      number_5.cancel();
      cout &lt;&lt; "\n";
      number_5.print();
  
  
      Q number_6 = number_1 * number_1;
      number_6.print();
      number_6.cancel();
      cout &lt;&lt; "\n";
      number_6.print();
  
      Q number_7 = number_2 / number_3;
      number_7.print();
      number_7.cancel();
      cout &lt;&lt; "\n";
      number_7.print();
  
  }
  </code></pre>
  
  <p>But compiler throws me an error:</p>
  
  <pre><code>/tmp/cce4zPM4.o: In function `operator*(Q&amp;, Q&amp;)':
  :(.text+0x170): undefined reference to `Q::Q()'
  /tmp/cce4zPM4.o: In function `operator/(Q&amp;, Q&amp;)':
  :(.text+0x1b0): undefined reference to `Q::Q()'
  /tmp/cce4zPM4.o: In function `operator+(Q&amp;, Q&amp;)':
  :(.text+0x1f0): undefined reference to `Q::Q()'
  /tmp/cce4zPM4.o: In function `operator-(Q&amp;, Q&amp;)':
  :(.text+0x230): undefined reference to `Q::Q()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I have no idea how to fix it. Can you help me ?</p>