title: C++ ifstream and ofstream overloading operator reading from file
body: <p>Hey guys I am trying to overload ifstream and ofstream but without any success.</p>
  
  <p>Header File:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  class Complex
  {
      private:
          double real;
          double imaginary;
      public:
          //constructors
          Complex();
          Complex(double newreal, double newimaginary);
          ~Complex();
          //setter
          void setReal(double newreal);
          void setImaginary(double newimaginary);
          //getter
          double getReal();
          double getImaginary();
          //print
          void print();
          //methods
          Complex conjugate();
          Complex add(Complex c2);
          Complex subtraction(Complex c2);
          Complex division(Complex c2);
          Complex multiplication(Complex c2);
  
  friend ifstream&amp; operator &gt;&gt; (ifstream&amp; in, Complex &amp;c1)
  {
  
      in &gt;&gt; c1;
  
      return in;
  }
  
  };
  </code></pre>
  
  <p>Test file:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include "Complex2.h"
  
  using namespace std;
  
  int main()
  {
      Complex c1;
  
      ifstream infile; 
      infile.open("in1.txt"); 
  
      cout &lt;&lt; "Reading from the file" &lt;&lt; endl; 
      infile &gt;&gt; c1;
  
      // write the data at the screen.
      infile.close();
  
      return 0;
  }
  </code></pre>
  
  <p>I didnt think the cpp file was important since the header file includes the ifstream.</p>
  
  <p>Everytime I run this program i get this error:</p>
  
  <pre><code>Reading from the file
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>I dont know how to fix it.</p>
  
  <p>Thank you very much.</p>