Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with my custom iterator
    primarykey
    data
    text
    <p>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".</p> <p>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.</p> <p>Here is my code, it should compile, but obviously my iterator is broken...</p> <p>EDIT: Since people are asking, the problem is that the iterator I implemented does not iterate over Bar</p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt; using namespace std; template&lt;class T&gt; class Foo { public: template&lt;class T&gt; class FooIterator : std::iterator&lt;std::forward_iterator_tag, T, ptrdiff_t, T*, T&amp;&gt; { public: Foo&lt;T&gt;* arrayPtr; int index, size; FooIterator(Foo&lt;T&gt;* arrayPtr, int size, int index) : arrayPtr(arrayPtr), size(size), index(index) {} T&amp; operator*() {return *(arrayPtr-&gt;operator[](index));} T* operator-&gt;() {return &amp;(operator*());} FooIterator &amp;operator++(){++index; return *this;} FooIterator operator++(int){FooIterator tmp(*this); ++(*this); return tmp;} bool operator!=(const FooIterator &amp;other) const {return other.index == index;} }; T** ts; int size; int index; typedef FooIterator&lt;T&gt; 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&amp; reference; typedef const T&amp; const_reference; typedef int size_type; typedef ptrdiff_t difference_type; }; template&lt;class T&gt; void Foo&lt;T&gt;::init(int size) { ts = new T*[size]; index = 0; } template&lt;class T&gt; Foo&lt;T&gt;::~Foo() { for(int i = 0; i &lt; index; i++) delete ts[i]; delete ts; } template&lt;class T&gt; void Foo&lt;T&gt;::insert(T* item) { ts[index++] = item; } template&lt;class T&gt; T* Foo&lt;T&gt;::operator[](int index) { return ts[index]; } struct Bar { public: Foo&lt;int&gt; 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 &lt;&lt; var &lt;&lt; endl; } } }; struct Bar2 { vector&lt;int&gt; nums; Bar2() { nums.push_back(4); nums.push_back(5); nums.push_back(6); for each (int var in nums) { cout &lt;&lt; var &lt;&lt; endl; } } }; int main () { Bar bar; /*for (int i = 0; i &lt; bar.nums.index; i++) { cout &lt;&lt; *bar.nums[i] &lt;&lt; endl; }*/ Bar2 bar2; cin.get(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload