title: Using cin >> and cout << to populate fields of a class C++
body: <p>I have a class in <code>MyClass.h</code> defined like this:</p>
  
  <pre><code>#ifndef MyClass_h
  #define MyClass_h
  
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class MyClass {
  public:
      string input
      void ReadFrom(istream &amp;is);
      void WriteTo(ostream &amp;os) const;
  };
  #endif /* MyClass_h */
  </code></pre>
  
  <p><code>MyClass.cpp</code> looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "MyClass.h"
  
  using namespace std;
  
  void MyClass::ReadFrom(istream &amp;is) {
      // put data into member 'input'
  }
  
  void MyClass::WriteTo(ostream &amp;os) const {
      // output data in member 'input'
  }
  
  istream&amp; operator &gt;&gt;(istream &amp;is, MyClass &amp;cls) {
      cls.ReadFrom(is);
      return is;
  }
  
  ostream&amp; operator &lt;&lt;(ostream &amp;os, const MyClass &amp;cls) {
      cls.WriteTo(os);
      return os;
  }
  </code></pre>
  
  <p><code>main.cpp</code> looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include "MyClass.h"
  
  using namespace std;
  
  int main(int argc, const char * argv[]) {
     MyClass c;
     std::cout &lt;&lt; "Enter some input" &lt;&lt; endl;
     std::cin &gt;&gt; c;
     return 0;
  }
  </code></pre>
  
  <p>What I am trying to accomplish is to override the <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators so that <code>std::cin</code> can simply read data into the class member(s), and then <code>std::cout</code> can spit out all of the data in those members.</p>
  
  <p>I do not want to use <code>friend</code> functions.</p>
  
  <p>Right now, I am getting an error around the <code>std::cin &gt;&gt; c;</code> line that says:</p>
  
  <blockquote>
    <p>Invalid operands to binary expression ('istream' (aka 'basic_istream&lt;char>') and 'MyClass')</p>
  </blockquote>