I have marked the area of interest in long asterisk marks.
The program executes properly, but it fails to adjust/update the size of the array, requiring me to do it manually.
// This program demonstrates the SimpleVector template .
#include <iostream>
#include "SimpleVector.h"
#include <vector>
usingnamespace std;
int main()
{
constint SIZE = 10; // Number of elements
int count; // Loop counter
//for push_back
int numInt;
double numDbl;
// Create a SimpleVector of ints.
SimpleVector<int> intTable(SIZE); //Xalls simple vector class..
// Create a SimpleVector of doubles .
SimpleVector<double> doubleTable(SIZE);
// Store values in the two SimpleVectors.
for (count = 0; count < SIZE; count++)
{
intTable[count] = (count * 2);
doubleTable[count] = (count * 2.14);
}
// Display the values in the SimpleVectors .
cout << "These values are in int Table:\n";
for (count = 0; count < SIZE; count++)
{cout << intTable[count] << " "; }
cout << endl;
cout << "These values are in doubleTable:\n";
for (count = 0; count < SIZE; count++)
cout << doubleTable[count] << " ";
cout << endl << endl;
// Use the standard + operator on the elements.
cout << "\nAdding 5 to each element of intTable"
<< " and doubleTable.\n";
for (count = 0; count < SIZE; count++) //increases existing elements by 5 units
{
intTable[count] = intTable[count] + 5;
doubleTable[count] = doubleTable[count] + 5.0;
}
// Display the values in the SimpleVectors.
cout << "These values are in intTable:\n";
for (count = 0; count < SIZE; count++)
cout << intTable[count] << " ";
cout << endl;
cout << "These values are in doubleTable: \n";
for (count = 0; count < SIZE; count++)
cout << doubleTable[count] << " ";
cout << endl << endl;
// Use the standard ++ operator on the elements.
cout << "\nIncrementing each element of intTable and" //increases each elements's value by 1
<< " doubleTable.\n";
for (count = 0; count < SIZE; count++)
{
intTable[count] ++;
doubleTable[count]++;
}
// Display the values in the SimpleVectors.
cout << "These values are in intTable:\n" ;
for (count = 0; count < SIZE; count++)
cout << intTable[count] << " ";
cout << endl;
cout << "These values are in doubleTable:\n";
for (count = 0; count < SIZE; count++)
cout << doubleTable[count] << " ";
cout << endl << endl;
cout << "Enter a number to insert to the int SimpleVector: ";
cin >> numInt;
intTable.push_back(numInt);
// Display the values in the SimpleVectors.
cout << "These values are in intTable:\n" ;
for (count = 0; count < SIZE + 1; count++) //had to change SIZE to, SIZE + 1
cout << intTable[count] << " ";
cout << endl;
//*****************************************************************************
//is there an alternative to doing 'SIZE + 1' in the for loop?
cout << "Enter a number to insert to the double SimpleVector: ";
cin >> numDbl;
doubleTable.push_back(numDbl);
for (count = 0; count < SIZE + 1; count++) //had to change SIZE to, SIZE + 1
cout << doubleTable[count] << " ";
cout << endl;
//*****************************************************************************
//After using the following pop_back function call(s), the subError, member function, is called
cout << "Pop an element from the int array..." << endl;
intTable.pop_back();
//intTable.pop_back();
//intTable.pop_back();
cout << "These values are in intTable:\n" ;
for (count = 0; count < SIZE; count++) //had to change SIZE to, SIZE + 1
cout << intTable[count] << " ";
cout << endl;
system("pause");
return 0;
}
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream> // Needed for bad_alloe exception
#include <new> // Needed for the exit function
#include <cstdlib>
#include <vector>
usingnamespace std;
template <class T>
class SimpleVector
{
private:
T *aptr; // To point to the allocated array
//*****************************************************************
// arraySize member variable is declared as an int. This is because
// it holds the size of the array, which will be an integer value,
// regardless of the data type of the array. This is also why
// the size member function returns an int.
//*****************************************************************
int arraySize; // Number of elements in the array
void memError(); // Handles memory allocation errors
void subError(); // Handles subscripts out of range
//T push_back(T num);
public:
// Default constructor
SimpleVector()
{ aptr = 0; arraySize = 0; }
// Constructor declaration
SimpleVector(int);
// Copy constructor declaration
SimpleVector(const SimpleVector &);
// Destructor declaration
~SimpleVector();
// Accessor to return the array size
int size() const
{ return arraySize; }
// Accessor to return a specific element
T getElementAt(int position);
void push_back(const T &);
void pop_back ();
// Overloaded [] operator declaration
T &operator[](constint &);
};
//************************************************************
// Constructor for SimpleVector class. Sets the size of the
// array and allocates memory for it.
//************************************************************
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
if (arraySize == 0) //added for PC 16_08
{
aptr = NULL;
return;
}
// Allocote memory for the array.
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array .
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
//*********************************************
// Copy Constructor for SimpleVector class.
//*********************************************
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &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 < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
//***************************************
// DestrUCtor for SimpleVector class.
//***************************************
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
//end constructors/destructors***************************************************************
//private members*****************************************************************************
template <class T>
void SimpleVector<T>::memError()
{
cout << "ERROR:Cannot allocate memory.\n";
system("pause");
exit(EXIT_FAILURE);
}
//************************************************************
// subError function. Displays an error message and
// terminates the program when a subscript is out of range.
//************************************************************
template <class T>
void SimpleVector<T>::subError()
{
cout << "ERROR: Subscript out of range.\n";
system("pause");
exit(EXIT_FAILURE);
}
//end private members*************************************************************************
//public members
template <class T>
void SimpleVector<T>::push_back(const T &num)
{
T *temp = new T[arraySize + 1];
for(int index = 0; index < arraySize; index++) // if arraySize == 0, this will not execute.
{
temp[index] = aptr[index];
}
temp[arraySize++] = num;
//delete [] aptr;
aptr = temp;
}
template <class T>
void SimpleVector<T>::pop_back()
{
T *temp = new T[arraySize];
for (int index = 0; index < arraySize; index++)
{
temp[index] = aptr[index];
}
temp[arraySize--];
//delete [] aptr;
aptr = temp;
}
//*****************************************************
// getElementAt function. The argument is a subscript.
// This function returns the value stored at the sub-
// cript in the array.
//*****************************************************
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
if (sub < 0 || sub >= 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 <class T>
T &SimpleVector<T>::operator[](constint &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
#endif
I think what Peter87 is saying is that when you insert an element into the vector using the pop_back function, then you change the size. However, you are still using the const int SIZE, which is 10, thus you are using the wrong size.
I did not look through all that code, but instead, try using the vector::size() member function, which returns the vector's size.