title: Input operator overloading with a function
body: <p>I am a bit new at C++ and I have to solve the following exercise:</p>
  
  <p>I have to create an object of class Sum(limit) where the user can input numbers until the sum of all the inputs reaches the limit.</p>
  
  <p>The main function has to look like this and cannot be changed:</p>
  
  <pre><code>int main (){
      Sum sum(100);
      std::cout &lt;&lt; "Enter numbers\n";
      do{
          std::cin &gt;&gt; sum;
      } while (!sum.exceededlimit());
      std::cout &lt;&lt; "The sum of all the components is : " &lt;&lt; sum() &lt;&lt; "\n";
      return 0;
  }
  </code></pre>
  
  <p>So I created the class in which I put an empty vector. For now, the problem that I have is how to overload the input operator so that the input can go directly in the vector of the object.</p>
  
  <p>This is what I have tried until now (I didn't try the limit part, I am just trying to add input to a vector for now) but it seems to have a problem with input>>c.newnumber(input);</p>
  
  <p>Thanks a lot if you take the time to help me.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class Sum {
      private:
          int limit;
      public:
          std::vector&lt;int&gt; vect;
          int newnumber(int x){
          vect.push_back(x);}
          Sum(int x);
  };
  
  std::istream &amp; operator&gt;&gt;(std::istream &amp;input, Sum &amp; c){
      input&gt;&gt;c.newnumber(input);
      return input;
  }
  
  
  Sum::Sum(int x) {
      limit=x;
  }
  
  int main () {
      Sum sum(100);
      std::cin &gt;&gt; sum;
      std::cin &gt;&gt; sum;
      std::cin &gt;&gt; sum;
      std::cin &gt;&gt; sum;
      std::cin &gt;&gt; sum;
      return 0;
  }
  </code></pre>