title: overloading Extraction and Insertion << >> operator c++
body: <p>I've been trying to overload the &lt;&lt; (cout)  and >> (cin) operators for awhile now for a class of complex numbers that I was given the prototypes for in a .h file to do.</p>
  
  <p>This is my attempt to do so in the class.cpp file:</p>
  
  <pre><code>std::istream &amp;operator&gt;&gt; (std::istream &amp;in, complex&amp; c) {
      double h, j;
      in &gt;&gt; h &gt;&gt; j;
  
      c.set(h,j);
  
      return in;
  }
  
  std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, complex c) {
      out &lt;&lt; c.getReal;
      out &lt;&lt; c.getImag;
  
      return out;
  }
  </code></pre>
  
  <ol>
  <li><p>This is really the first time I've seen the <code>std:: istream</code> and <code>std:: ostream</code> rather than cout and cin and i'm not really sure how <code>cout &lt;&lt;</code>  and  <code>cin &gt;&gt;</code> are all related</p></li>
  <li><p>I try testing it on the main.cpp file but i get errors</p></li>
  <li><p>The <code>&gt;&gt;</code> should absorb a complex argument into the complex class argument and change the value of private members <code>real</code> and <code>imag</code> </p></li>
  <li><p>Not sure if I'm supposed to use my set function here</p></li>
  </ol>
  
  <p>This is the protype .h file filled I was given:    </p>
  
  <pre><code>#ifndef COMPLEX_H
  #define COMPLEX_H
  
  
  class complex {
  
  public:
      complex();
      complex(double a);
      complex(double a, double b);
      complex(int a, int b);
      void print() const;
      void set(double a = 0, double b = 0);
      void set(int a = 0, int b = 0);
      double getReal() const;
      double getImag() const;
      void get(double&amp;, double&amp;) const;
  
      complex operator+ (double&amp; x);
      complex operator+ (complex&amp;);
      complex operator+= (complex&amp;);
      complex operator+= (int&amp;);
      complex operator++ (int);
      complex operator++ ();
      complex operator- (double&amp;);
      complex operator- (complex&amp;);
      complex operator-= (complex&amp;);
      complex operator-= (double&amp;);
      complex operator-- (int);
      complex operator-- ();
      complex operator* (complex&amp;);
      complex operator* (double&amp;);
      complex operator*= (complex&amp;);
      complex operator*= (double&amp;);
      complex operator/ (complex&amp;);
      complex operator/= (complex&amp;);
      complex operator/= (double);
      complex operator/ (double);
      void operator= (const complex&amp;);
      bool operator== (complex&amp;);
      bool operator!=(complex &amp;c);
  
      friend std::istream &amp;operator&gt;&gt; (std::istream &amp;in, complex&amp; c);
      friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, complex c);
  
      /*complex conj() const;
      double norm() const;
      double modulus() const;
      */
  private:
      double real;
      double imag;
  };
  
  #endif // COMPLEX_H
  </code></pre>
  
  <p>Can someone show me how to properly overload for this?</p>
  
  <p>Ok so I'm now trying for output a + bi :</p>
  
  <pre><code>std::ostream&amp; operator&lt;&lt; (std::ostream&amp; out, complex const&amp; c) {
      return out &lt;&lt; c.getReal() &lt;&lt; "+" &lt;&lt; c.getImag() &lt;&lt; "i";
  }
  </code></pre>
  
  <p>and for input:</p>
  
  <pre><code>std::istream&amp; operator&gt;&gt; (std::istream&amp; in, complex&amp; c) {
      double h, j;
      if (in &gt;&gt; h &gt;&gt; "+" &gt;&gt; j &gt;&gt; "i") {
          c.set(h, j);
      }
      return in;
  }
  </code></pre>
  
  <p>However I get the following error when I compile:</p>
  
  <p>This is for line 181 of my complex.cpp file(class complex implementation file) where <code>if (in &gt;&gt; h &gt;&gt; "+" &gt;&gt; j &gt;&gt; "i") {</code>is located of the above function definition is located:</p>
  
  <pre><code>binary '&gt;&gt;': no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion) 
  </code></pre>
  
  <p>the following are all for line 45(Note each error is separate,total of 7 for this line) of my complex.h file where <code>friend std::istream &amp;operator&gt;&gt; (std::istream &amp;in, complex&amp; c);</code> protoype is located.</p>
  
  <pre><code>'istream':is not a member of 'std'
  
  syntax error missing ';' before '&amp;'
  
  'istream':'friend' not permitted on data declarations
  
  missing type specifier-int assumed. Note:C++ does not support default-int
  
  unexpected token(s) preceding';'
  
  
  namespace "std" has no member "istream"
  
  namespace "std" has no member "istream"
  </code></pre>
  
  <p>the following are for line 46 of my complex.h file where </p>
  
  <pre><code>friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, complex c);
  </code></pre>
  
  <p>is located </p>
  
  <pre><code>'ostream': is not a member of 'std'
  
  syntax error: missing ';' before '&amp;'
  
  'ostream':'friend' not permitted on data declarations
  
  missing type specifier -int assumed.Note: C++ does not support default-int
  
  unexpected token(s) preceding ';'
  
  namespace "std" has no member "ostream"
  
  namespace "std" has no member "ostream"
  </code></pre>
  
  <p>I noticed both are the same type of errors. Note I have </p>
  
  <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  </code></pre>
  
  <p>both on the complex.cpp file and the main.cpp file</p>