title: Creating a "push_back" Function for Array
body: <p>My assignment is to create push_back and pop_back functions for a SimpleVector which have to do basically what you imagine they have to do. I'm trying to add an element to an existing array in this fashion but it isn't working. It adds 0 instead of the number I pass in in main. Any ideas as to why?</p>
  
  <blockquote>
    <p><strong>SimpleVector.h</strong></p>
  </blockquote>
  
  <pre><code>// SimpleVector class template
  #ifndef SIMPLEVECTOR_H
  #define SIMPLEVECTOR_H
  #include &lt;iostream&gt;
  #include &lt;new&gt;       // Needed for bad_alloc exception
  #include &lt;cstdlib&gt;   // Needed for the exit function
  
  using namespace std;
  
  template&lt;class T&gt;
  class SimpleVector
  {
  private:
      T *aptr;          // To point to the allocated array
      int arraySize;    // Number of elements in the array
      void memError();  // Handles memory allocation errors
      void subError();  // Handles subscripts out of range
  
  public:
      // Default constructor
      SimpleVector()
      {
          aptr = 0;
          arraySize = 0;
      }
  
      // Constructor declaration
      SimpleVector(int);
  
      // Copy constructor declaration
      SimpleVector(const SimpleVector &amp;);
  
      // Destructor declaration
      ~SimpleVector();
  
      // Accessor to return the array size
      int size() const
      {
          return arraySize;
      }
  
      // Accessor to return a specific element
      T getElementAt(int position);
  
      // Overloaded [] operator declaration
      T &amp;operator[](const int &amp;);
  
      void push_back(SimpleVector, int);
  
      void pop_back(SimpleVector, int);
  };
  
  //***********************************************************
  // Constructor for SimpleVector class. Sets the size of the *
  // array and allocates memory for it.                       *
  //***********************************************************
  
  template&lt;class T&gt;
  SimpleVector&lt;T&gt;::SimpleVector(int s)
  {
      arraySize = s;
      // Allocate memory for the array.
      try
      {
          aptr = new T[s];
      }
      catch (bad_alloc)
      {
          memError();
      }
  
      // Initialize the array.
      for (int count = 0; count &lt; arraySize; count++)
          *(aptr + count) = 0;
  }
  
  //*******************************************
  // Copy Constructor for SimpleVector class. *
  //*******************************************
  
  template&lt;class T&gt;
  SimpleVector&lt;T&gt;::SimpleVector(const SimpleVector &amp;obj)
  {
      // Copy the array size.
      arraySize = obj.arraySize;
  
      // Allocate memory for the array.
      aptr = new T[arraySize];
      if (aptr == 0)
          memError();
  
      // Copy the elements of obj's array.
      for (int count = 0; count &lt; arraySize; count++)
          *(aptr + count) = *(obj.aptr + count);
  }
  
  //**************************************
  // Destructor for SimpleVector class.  *
  //**************************************
  
  template&lt;class T&gt;
  SimpleVector&lt;T&gt;::~SimpleVector()
  {
      if (arraySize &gt; 0)
          delete[] aptr;
  }
  
  //*******************************************************
  // memError function. Displays an error message and     *
  // terminates the program when memory allocation fails. *
  //*******************************************************
  
  template&lt;class T&gt;
  void SimpleVector&lt;T&gt;::memError()
  {
      cout &lt;&lt; "ERROR:Cannot allocate memory.\n";
      exit(EXIT_FAILURE);
  }
  
  //***********************************************************
  // subError function. Displays an error message and         *
  // terminates the program when a subscript is out of range. *
  //***********************************************************
  
  template&lt;class T&gt;
  void SimpleVector&lt;T&gt;::subError()
  {
      cout &lt;&lt; "ERROR: Subscript out of range.\n";
      exit(EXIT_FAILURE);
  }
  
  //*******************************************************
  // getElementAt function. The argument is a subscript.  *
  // This function returns the value stored at the sub-   *
  // script in the array.                                  *
  //*******************************************************
  
  template&lt;class T&gt;
  T SimpleVector&lt;T&gt;::getElementAt(int sub)
  {
      if (sub &lt; 0 || sub &gt;= arraySize)
          subError();
      return aptr[sub];
  }
  
  //*******************************************************
  // Overloaded [] operator. The argument is a subscript. *
  // This function returns a reference to the element     *
  // in the array indexed by the subscript.               *
  //*******************************************************
  
  template&lt;class T&gt;
  T &amp;SimpleVector&lt;T&gt;::operator[](const int &amp;sub)
  {
      if (sub &lt; 0 || sub &gt;= arraySize)
          subError();
      return aptr[sub];
  }
  #endif
  
  template&lt;class T&gt;
  void SimpleVector&lt;T&gt;::push_back(SimpleVector obj, int newval)
  {
      arraySize = obj.arraySize + 1;
  
      // Allocate memory for the array.
      aptr = new T[arraySize];
      if (aptr == 0)
          memError();
  
      // Copy the elements of obj's array.
      for (int count = 0; count &lt; (arraySize - 1); count++)
          *(aptr + count) = *(obj.aptr + count);
  
      obj.aptr[(arraySize)] = newval;
  
  }
  </code></pre>
  
  <blockquote>
    <p><strong>main.cpp</strong></p>
  </blockquote>
  
  <pre><code>// This program demonstrates the SimpleVector template.
  #include &lt;iostream&gt;
  #include "SimpleVector.h"
  using namespace std;
  
  int main()
  {
      const int SIZE = 10; // Number of elements
      int amtToPrnt = 10;
      int count; // Loop counter
  
  // Create a SimpleVector of ints.
      SimpleVector&lt;int&gt; intTable(SIZE);
  
  // Store values in the two SimpleVectors.
      for (count = 0; count &lt; SIZE; count++)
      {
          intTable[count] = (count * 2);
      }
  
  // Display the values in the SimpleVectors.
      cout &lt;&lt; "These values are in intTable:\n";
      for (count = 0; count &lt; amtToPrnt; count++)
          cout &lt;&lt; intTable[count] &lt;&lt; " ";
      cout &lt;&lt; endl;
  
      intTable.push_back(intTable, 20);
      cout &lt;&lt; "These values are in intTable after adding one more value:\n";
      for (count = 0; count &lt; ++amtToPrnt; count++)
          cout &lt;&lt; intTable[count] &lt;&lt; " ";
      cout &lt;&lt; endl;
  
      /*
       intTable.push_back(intTable, 22);
       cout &lt;&lt; "These values are in intTable after adding one more value:\n";
       for (count = 0; count &lt; ++amtToPrnt; count++)
       cout &lt;&lt; intTable[count] &lt;&lt; " ";
       cout &lt;&lt; endl;
  
       intTable.push_back(intTable, 24);
       cout &lt;&lt; "These values are in intTable after adding one more value:\n";
       for (count = 0; count &lt; ++amtToPrnt; count++)
       cout &lt;&lt; intTable[count] &lt;&lt; " ";
       cout &lt;&lt; endl;
       */
      return 0;
  }
  </code></pre>