0

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.

3
  • Your iterator constructor wants a pointer and two integers. You are giving it something else in both cases. Commented Feb 8, 2018 at 22:45
  • @SergeyA Oh, I should change myArray* to ItemType* right? Commented Feb 8, 2018 at 22:50
  • Please put the whole minimal reproducible example into one window. Don't make us assemble the pieces in order to run the code. Commented Feb 8, 2018 at 22:56

1 Answer 1

1

A few fixes to keep you going:

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(const int array_size = INITIAL_CAPACITY) {
        current_capacity = array_size;
        num_items = 0;
        arrayData = new ItemType[array_size](); // Allocate. And value/zero-initialialize, note () at the end.
    }
    class Iterator {
    private:
        ItemType* element_; // Points to the current element.
    public:
        Iterator(ItemType* element)
            : element_(element)
        {}
    };

    Iterator begin() {
        return Iterator(arrayData);
    }
    Iterator end() {
        return Iterator(arrayData + num_items);
    }
};

Notes:

  • Inside myArray class template, myArray refers to myArray<ItemType>, no need to specify the template argument.
  • Use new instead of malloc. new initialises the elements for you and throws on failure.
  • You may like your class names to start with a capital letter; and variables and functions to start with lowercase one. This is a pretty common convention in C++ and other languages. This helps reading code.

You will need to complete this class by adding the destructor, copy constructor and assignment, and implementing the iterator.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I didn't even catch that I called myArray* instead of ItemType...Good catch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.