-2

So, I've been trying to figure out how custom iterators work, and after investing so many hours and still coming up short, I've decided to ask the community at large: What am I doing wrong? Please help, explanations would be nice, but not necessary if it is "minor".

As a side question, does anyone know what the requirements are to get "show as Collection Association" to work in the class diagram thing in MSVS? does it just not work with c++? Even my bar2 is not a collection of ints as far as it is concerned.

Here is my code, it should compile, but obviously my iterator is broken...

EDIT: Since people are asking, the problem is that the iterator I implemented does not iterate over Bar

#include <vector>
#include <iostream>

using namespace std;

template<class T> 
class Foo
{
public:
    template<class T>

    class FooIterator : std::iterator<std::forward_iterator_tag, T, ptrdiff_t, T*, T&>
    {
    public:
        Foo<T>* arrayPtr;
        int index, size;
        FooIterator(Foo<T>* arrayPtr, int size, int index) : arrayPtr(arrayPtr), size(size), index(index) {}

        T& operator*() {return *(arrayPtr->operator[](index));}
        T* operator->() {return &(operator*());}

        FooIterator &operator++(){++index; return *this;}
        FooIterator operator++(int){FooIterator tmp(*this); ++(*this); return tmp;}
        bool operator!=(const FooIterator &other) const {return other.index == index;}
    };

    T** ts;
    int size;
    int index;

    typedef FooIterator<T> fooiterator;
    fooiterator begin(){return fooiterator(this, size, 0);}
    fooiterator end(){return fooiterator(this, size, size);}

    ~Foo();

    void init(int size);
    void insert(T* item);
    T* operator[](int index);

    typedef T          value_type;
    typedef T*         pointer;
    typedef const T*   const_pointer;
    typedef T&         reference;
    typedef const T&   const_reference;
    typedef int        size_type;
    typedef ptrdiff_t  difference_type;
};

template<class T>
void Foo<T>::init(int size)
{
    ts = new T*[size];
    index = 0;
}

template<class T>
Foo<T>::~Foo()
{
    for(int i = 0; i < index; i++)
        delete ts[i];
    delete ts;
}

template<class T>
void Foo<T>::insert(T* item)
{
    ts[index++] = item;
}

template<class T>
T* Foo<T>::operator[](int index)
{
    return ts[index];
}

struct Bar
{
public:
    Foo<int> nums;
    Bar()
    {
        nums.init(3);
        int val = 1;
        nums.insert(new int(1));
        nums.insert(new int(2));
        nums.insert(new int(3));

        for each (int var in nums)
        {
            cout << var << endl;
        }
    }
};

struct Bar2
{
    vector<int> nums;
    Bar2()
    {

        nums.push_back(4);
        nums.push_back(5);
        nums.push_back(6);
        for each (int var in nums)
        {
            cout << var << endl;
        }
    }
};

int main ()
{
    Bar bar;
    /*for (int i = 0; i < bar.nums.index; i++)
    {
        cout << *bar.nums[i] << endl;
    }*/
    Bar2 bar2;
    cin.get();

    return 0;
}
epsalon
  • 2,294
  • 12
  • 19
paycheck87
  • 47
  • 1
  • 6
  • I will ask the same question... could you tell us what's wrong with it? post compile eroor / problem description – Karoly Horvath Sep 24 '12 at 16:40
  • No, it does not compile. There is an (unnecessarily shadowed template parameter) and `for each` is not C++. – pmr Sep 24 '12 at 16:43
  • @pmr, for each is [visual c++ feature](http://stackoverflow.com/questions/197375/visual-c-for-each-portability) – Lol4t0 Sep 24 '12 at 16:44
  • the problem is that the for each in bar1 does not actually print anything. The iterator is not iterating... – paycheck87 Sep 24 '12 at 16:48
  • [this](http://stackoverflow.com/a/840055/942596) seems like a good way to start implementing your own iterator. – andre Sep 24 '12 at 17:30

1 Answers1

3

One obvious problem is that operator!= actually tests for equality.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165