title: How do I overload the << operator correctly in order to return an int value?
body: <p>I can't seem to overload the <code>&lt;&lt;</code> operator correctly. This is the code I have so far and the instructions for my assignment will be down below. If you point out other mistakes I made that would be kind, BUT my questions is how do I correctly overload my <code>&lt;&lt;</code> operator in my case?</p>
  
  <p><strong>INTCOLLECTION.h:</strong></p>
  
  <pre><code>#ifndef INTCOLLECTION_H
  #define INTCOLLECTION_H
  
  // Allocate memory in chunks of ints of this size.
  const int CHUNK_SIZE = 5;
  
  class IntCollection
  {
    private:
    // The number of ints currently stored in the int
      int size;
    // the total number of elements available for storage
    // in the data array
      int capacity;
    // A pointer to the dynamically allocated data array
      int* data;
    // a private member function to allocate more memory 
    // if necessary
      void addCapacity();
    public:
    // Constructor
      IntCollection();
    // Destructor
      ~IntCollection();
    // Copy constructor:
      IntCollection(const IntCollection &amp;c);
  
      void add(int value);
      int get(int index);
      int getSize();
      IntCollection&amp; operator=(const IntCollection &amp;c);
      bool operator==(const IntCollection &amp;c);
      IntCollection&amp; operator&lt;&lt;(int value);
  };
  
  #endif
  </code></pre>
  
  <p><strong>INTCOLLECTION.cpp:</strong></p>
  
  <pre><code>#include "IntCollection.h"
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  IntCollection::IntCollection()
  {
    // Initialize member data to reflect an empty 
    // IntCollection
    size = capacity = 0;
    data = NULL;
  }
  
  IntCollection::~IntCollection()
  {
    delete [] data;
  }
  
  IntCollection::IntCollection(const IntCollection &amp;c)
  {
    size = c.size;
    capacity = c.capacity;
    data = c.data;
  
    for(int i = 0; i &lt; c.size; i++)
    {
      data[i] = c.data[i];
    }
  }
  
  void IntCollection::addCapacity()
  {
    // Create a new, bigger buffer, copy the current data to
    // it, delete the old buffer, and point our data 
    // pointer to the new buffer
    int *newData;
    data = new int[capacity];
    capacity += CHUNK_SIZE;
    newData = new int[capacity];
  
    for(int i = 0; i &lt; size; i++)
    {
      newData[i] = data[i];
      delete [] data;
      data = newData;
    }
  }
  
  void IntCollection::add(int value)
  {
    //first, allocate more memory if we need to
    if(size == capacity)
    {
      addCapacity();
    }
    //Now, add the data to our array and increment size
    data[size++] = value;
  }
  
  int IntCollection::get(int index)
  {
    if (index &lt; 0 || index &gt;= size)
    {
      cout &lt;&lt; "ERROR: get() trying to access index out of range.\n";
      exit(1);
    }
    return data[index];
  }
  
  int IntCollection::getSize()
  {
    return size;
  }
  
  IntCollection&amp; IntCollection::operator=(const IntCollection &amp;c)
  {
    size = c.size;
    capacity = c.capacity;
    data = c.data;
  
    return *this;
  }
  
  bool IntCollection::operator==(const IntCollection &amp;c)
  {
    if((size == c.size) &amp;&amp; (capacity == c.capacity))
    {
      for(int m = 0; m &lt; size; m++)
      {
        if(data[m] == c.data[m])
        {
          continue;
        }
        else
        {
          return false; 
        }
      }
    }
    return true;
  }
  
  IntCollection&amp; IntCollection::operator&lt;&lt;(int value)
  {
    return value; // &lt;-- THIS IS WHERE I GET LOST!
    /* I also tried changing the parameters to
    (IntCollection &amp;b, int value) to return b
    but my teacher wants only one parameter
    and it wasn't working that way either. */
  }
  </code></pre>
  
  <p><strong>INSTRUCTIONS:</strong></p>
  
  <blockquote>
    <p>For this assignment you will add a copy constructor, a destructor, and three overloaded operators to the IntCollection class.  In the design diagram below, the black member functions represent code that has already been implemented.  You will be implementing the green items.  Each item that you will be adding to the class is described below the diagram.</p>
    
    <p>Private:</p>
    
    <p>int size            // the number of ints currently stored in the int collection</p>
    
    <p>int capacity        // total number of elements available in data array</p>
    
    <p>int* data           // a pointer to the dynamically allocated data array</p>
    
    <p>void addCapacity(); // private function to allocate more memory if necessary</p>
    
    <p>Public:</p>
    
    <p>IntCollection()</p>
    
    <p>~IntCollection()</p>
    
    <p>IntCollection(const IntCollection &amp;c)</p>
    
    <p>void add(int value)</p>
    
    <p>int get(int index)</p>
    
    <p>int getSize()</p>
    
    <p>IntCollection&amp; operator=(const IntCollection &amp;c)</p>
    
    <p>bool operator==(const IntCollection &amp;c)</p>
    
    <p>IntCollection&amp; operator&lt;&lt;(int value)</p>
    
    <p>The Copy Constructor.  The copy constructor should perform a deep copy of the argument object, i.e. it should construct an IntCollection with the same size and capacity as the argument, with its own complete copy of the argument's data array.</p>
    
    <p>The Assignment Operator (=). The assignment operator should also perform a deep copy of the argument object.  It must return itself (or more efficiently, a reference to itself) in order to support multiple assignments on the same line, e.g. a = b = c.  If you implement your assignment operator first it could be used in the copy constructor, but this is not a requirement.</p>
    
    <p>The Is Equals operator (==). The "is equals" operator should return true if the argument object has the same size as the receiving object, and the values in both objects’ data arrays are identical.</p>
    
    <p><strong>The insertion operator (&lt;&lt;). The insertion operator should add the int parameter into the receiving IntCollection.  The functionality is exactly the same as the add() function, i.e. add ints to the collection.  Note, however, that this function must return a reference to itself in order to support multiple insertions on the same line, e.g. c &lt;&lt; 45 &lt;&lt; -210.  Unlike the assignment operator, this return must be done by reference, because each insertion actually modifies the IntCollection object, and insertion is done from left to right.</strong> </p>
    
    <p>The destructor. Function add() calls addCapacity() to allocate memory when it needs more room.  Nowhere in this program is the memory deallocated with delete [], which means we have a memory leak!  Add a destructor which correctly handles this.  </p>
    
    <p>addCapacity.  Note that addCapacity() is a private member function.  What happens if you try to call it from outside the class, i.e. by adding the line below to main()?</p>
  </blockquote>