I am trying to implement my own Iterator class, but am having trouble with the end function (to point to the end of the Iterator). Here is most of the implementation from the .h file:
template <typename ItemType>
class myArray
{
private:
ItemType* arrayData; // pointer to ALL THE DATA
static const size_t INITIAL_CAPACITY = 1;
size_t current_capacity;
size_t num_items; // number of items in the array being held
public:
myArray <ItemType>(const int array_size = INITIAL_CAPACITY) { // constructor
current_capacity = array_size;
num_items = 0;
arrayData = (ItemType*)malloc(sizeof(ItemType) * array_size);
if (arrayData == NULL) {
std::cout << "Error allocating memory" << std::endl;
throw arrayData;
}
memset(arrayData, 0, sizeof(ItemType) * array_size);
}
class Iterator {
private:
myArray* marker; //points to my array
int index; // location in my array
int mode;
public:
Iterator(myArray* vect, int index = 0, int mode = 0);
};
Iterator begin() {
return Iterator(arrayData, 0, 0);
}
Iterator end() {
return Iterator(std::begin(myArray& arrayData), num_items, 0);
} // last of array
};
Whenever I compile it just to build it, I get the following error:
<function-style-cast>': cannot convert from 'int *' to 'myArray<int>::Iterator
I'm pretty sure the problem lies either in the constructor, or in how I'm calling the constructor of the Iterator, but I can't seem to figure it out.
Here's the constructor:
template<typename ItemType>
myArray<ItemType>::Iterator::Iterator(myArray* vect, int index, int mode)
{
this->marker = (ItemType*)malloc(sizeof(ItemType)) * vect;
if (marker == NULL) {
std::cout << "Error allocating memory" << std::endl;
throw marker;
}
memset(marker, 0, sizeof(ItemType) * vect);
this->index = index;
this->mode = mode;
}
=============================================================
I have tried changing the constructor to this:
template<typename ItemType>
myArray<ItemType>::Iterator::Iterator(myArray* vect, int index, int mode)
{
this->marker = vect;
this->index = index;
this->mode = mode;
}
And the subsequent call to end as well:
Iterator end() {
return Iterator(arrayData + num_items); //This should give me the end of the array right?
}
I get a different error when doing it this way, so I'm not sure what's going wrong. Is my implementation of the constructor correct, or is it somewhere else in my code? I think it has to do with how I'm calling the constructor. Any help would be appreciated. None of the posts I could find about this were helpful to me, so I turned you guys.