#include <iostream>
#include "SimpleVector.h"
void* thisReturnsVoidPointer(SimpleVector<int> theVector);
int main()
{
SimpleVector<int> theVector(3);
void* theVectorVoidPtr = thisReturnsVoidPointer(theVector);
for (int i = 0; i < 3; i++)
{
theVector[i] = static_cast<SimpleVector<int>*>(theVectorVoidPtr)->getElementAt(i);
}
for (int i = 0; i < 3; i++)
{
cout << theVector[i];
}
cin.get();
}
void* thisReturnsVoidPointer(SimpleVector<int> theVector)
{
void* theVectorVoidPtr;
for (int i = 0; i < 3; i++)
{
theVector[i] = i+1;
}
theVectorVoidPtr = &theVector;
return theVectorVoidPtr;
}
I want to copy the array that the void pointer is supposed to point to into a SimpleVector because I don't like the void pointer... it is confusing. But I get a subscript error, which is coming from this part of the SimpleVector template... How can I make the above code work?
1 2 3 4 5 6 7
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
// SimpleVector class template
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new> // Needed for bad_alloc exception
#include <cstdlib> // Needed for the exit function
usingnamespace std;
template <class T>
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
//int back; // holds index of last array element
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);
// Overloaded [] operator declaration
T &operator[](constint &);
//calling paramater dataItem instead of value (like in the stl vector) so that
//in comments I won't have to say weird things like "the value of value"
T* push_back(T dataItem);
};
//***********************************************************
// 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;
// Allocate memory for the array.
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array.
//This code is causing problems with making a vector of acounts
//because an account object can't be set to zero
//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;
}
//*******************************************************
// memError function. Displays an error message and *
// terminates the program when memory allocation fails. *
//*******************************************************
template <class T>
void SimpleVector<T>::memError()
{
cout << "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 <class T>
void SimpleVector<T>::subError()
{
cout << "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- *
// 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];
}
template <class T>
T* SimpleVector<T>::push_back(T dataItem)
{
int newArraySize;
// Copy the array size and add 1 to hold the new element
//new array size is one bigger than the size of original array
newArraySize = this->arraySize + 1;
T* newTempArrayPtr;
// Allocate memory for the new array.
newTempArrayPtr = new T [newArraySize];
if (aptr == 0)
{
memError();
}
//copy elements of original array to new temporary array
for (int i = 0; i < arraySize; i++)
{
newTempArrayPtr[i] = aptr[i];
}
//set value in last index of newTempArrayPtr to be value of dataItem
newTempArrayPtr[newArraySize - 1] = dataItem;
//make aptr point to address of the array that newTempArrayPtr points to
//(Tried to do this with function returning void but it doesn't work.)
//*aptr = *newTempArrayPtr;
//increment member variable arraySize so that it will contain the size of the new array
arraySize++;
aptr = new T [newArraySize];
for (int i = 0; i < newArraySize; i++)
{
aptr[i] = newTempArrayPtr[i];
}
//delete the temporary pointer to the new array. it is not needed any more because
//aptr points to the new array now.
//delete [] newTempArrayPtr;
////the variable arraySize will contain the old array's size.
////arraySize will be changed to the new array size near end of the
////function
// // // Copy the elements of obj's array
// //for(int count = 0; count < arraySize; count++)
// // *(aptr + count) = *(obj.aptr + count);
// ////make arraySize 1 element bigger than it was before by changing it to newArraySize
// //arraySize = newArraySize;
// ////Then add the dataItem to the end
// //*(aptr + (arraySize - 1)) = dataItem;
return aptr;
}
#endif
You are returning a pointer to a copy of the vector you passed to the function, pass it by reference. void* thisReturnsVoidPointer(SimpleVector<int>& theVector);
I realized that in simplifying my problem into the above code... I didn't accurately represent the problem. I want to make size the SimpleVector dynamically by reading in the size from a file in the function. I still have the problem but I realized I can make my life easier by making a dynamic array in the function rather than a SimpleVector and then copying everything into the SimpleVector once I have returned the array. Then I wouldn't have to mess with this confusing void pointer stuff... Still. I am really curious about how I would do this with the void pointer. Is it even possible to do this? This is what I've been messing with but all I can do is get it to print out the lowest number the computer can hold 3 times. Why does it do that?