I'm working on a template Class in C++ similar to the ArrayList in java (yes I know vector does the same thing, this is not a utilitarian coding project).
I figured it would be useful to have a Constructor for my ArrayList class that takes another ArrayList as an argument to seed the ArrayList. But when I try and write the constructor I get this error
invalid constructor; you probably meant 'ArrayList<T> (const ArrayList<T>&)'
Does this mean that the ArrayList has to be a constant? And why do I need the addressof operator?
I'm still learning the ropes of C++ so I'm a bit confused.
The prototypes are here:
ArrayList(ArrayList<T> list);
ArrayList(ArrayList<T> list, int size);
The code is here:
/**
* Creates an ArrayList of type T that is twice the
* size of the passed in ArrayList, and adds all elements
* from the passed ArrayList<T> list, to this ArrayList.
*
* Runs in O(n) time, where n = the size of list.
*
* @param list the ArrayList to use as a seed for this ArrayList.
*/
template<class T>
ArrayList<T>::ArrayList(ArrayList<T> list) {
array = new T[list.getSize() * 2];
capacity = list->getSize() * 2;
size = list->getSize();
for (int i = 0; i < list->getSize(); i++) {
array[i] = list->get(i);
}
}
Edit The below code gets no errors, while the above does.....
/**
* Creates an ArrayList of type T that has a capacity equal to the passed
* in theCapacity parameter. This ArrayList starts with the passed ArrayList.
*
* Note: If the passed in capacity is smaller than the size of the passed in
* ArrayList, then the capacity is set to twice the size of the
* passed ArrayList.
*
* @param list the ArrayList to use as a seed for this ArrayList.
* @param theCapacity the capacity for this ArrayList.
*/
template<class T>
ArrayList<T>::ArrayList(ArrayList<T> list, int theCapacity) {
if (theCapacity >= list->getSize()) {
array = new T[theCapacity];
capacity = theCapacity;
}
else {
array = new T[list->getSize() * 2];
capacity = list->getSize() * 2;
}
size = list->size;
for (int i = 0; i < size; i++) {
array[i] = list->get(i);
}
}