Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There already is <a href="http://en.cppreference.com/w/cpp/iterator/iterator_traits" rel="nofollow">std::iterator_traits</a> in the standard library in <code>&lt;iterator&gt;</code>. Your <code>using namespace std;</code> makes the one in the header visible along with your user defined one. <strong>Remove</strong> <code>using namespace std;</code> from the code.</p> <p>The other big problem is places like here: <code>reference operator*()</code>. With templates, when you use <code>reference</code> the compiler only looks inside of <code>Checked_iter</code> to see if it exists. You need to fully qualify it like this: <code>typename iterator_traits&lt;Iter&gt;::reference operator*()</code> or use what is called the <strong>injected class name</strong>, i.e. <code>typename Checked_iter::reference operator*()</code>. Otherwise, the compiler will tell you that it does not exist. BTW, <code>reference</code>, etc. is what is known a a <strong>dependent name</strong>.</p> <p>Once the above errors are fixed, you will need to include <code>&lt;stdexcept&gt;</code> to get <code>std::out_of_range</code>, etc. And prepend everything with <code>std::</code>.</p> <p>Also, in <code>difference_type index() {return curr-c.begin();}</code>, <code>c</code> is a <strong>pointer</strong>, so you need to change to <code>c-&gt;begin()</code>.</p> <p>Lots of errors... I got a <a href="http://liveworkspace.org/code/4xvsYI%2437" rel="nofollow">compilable program</a>:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;iterator&gt; #include &lt;exception&gt; #include &lt;cstddef&gt; #include &lt;stdexcept&gt; template&lt;class Iter&gt; struct iterator_traits{ typedef typename Iter::iterator_category iterator_category; typedef typename Iter::value_type value_type; typedef typename Iter::difference_type difference_type; typedef typename Iter::pointer pointer; typedef typename Iter::reference reference; }; // specialization for pointer //template&lt;class T&gt; struct iterator_traits&lt;T*&gt; //{ // typedef ptrdiff_t difference_type; // typedef T value_type; // typedef T* pointer; // typedef T&amp; reference; // typedef random_access_iterator_tag iterator_category; //}; template&lt;class Cat, class T, class Dist=ptrdiff_t, class Ptr=T*, class Ref=T&amp;&gt; struct iterator { typedef Cat iterator_category; typedef T value_type; typedef Dist difference_type; typedef Ptr pointer; typedef Ref reference; }; template&lt;class Ran&gt; typename iterator_traits&lt;Ran&gt;::difference_type dist_helper(Ran first, Ran last, std::random_access_iterator_tag) { return last-first; //rely on random access } template&lt;class In &gt; typename iterator_traits&lt;In&gt;::difference_type distance(In first, In last) { return dist_helper(first, last, iterator_traits&lt;In&gt;::iterator_category()); } //Code template &lt;class Cont, class Iter=typename Cont::iterator&gt; class Checked_iter : public iterator_traits&lt;Iter&gt;{ //deriving from iterator_traits Iter curr; Cont* c; //.... public: void valid(Iter p) { if ( c-&gt;end() == p) return; for(Iter pp= c-&gt;begin(); pp != c-&gt;end(); ++pp){ if (pp==p) return; } throw std::out_of_range("out of range"); } friend bool operator==(const Checked_iter&amp; i, const Checked_iter&amp; j) { return i.c == j.c &amp;&amp; i.curr == j.curr; } //no default initialzer //use defaul t copy constructor and cop assignment. Checked_iter(Cont&amp; x, Iter p):c(&amp;x), curr(p) {valid(p);} typename iterator_traits&lt;Iter&gt;::reference operator*() { if (curr == c-&gt;end()) throw std::out_of_range("out of range"); return *curr; } typename iterator_traits&lt;Iter&gt;::pointer operator-&gt;() { return &amp;*curr; // checked by * } Checked_iter operator+(typename iterator_traits&lt;Iter&gt;::difference_type d ) // for random access iterators only { if(c-&gt;end() - curr &lt;= d ) throw std::out_of_range("out of range"); return Checked_iter(c ,curr+d ); } typename iterator_traits&lt;Iter&gt;::reference operator[](typename iterator_traits&lt;Iter&gt;::difference_type d ) // for random access iterators only { if (c-&gt;end() - curr &lt;= d) throw std::out_of_range("out of range"); return c[d]; } Checked_iter&amp; operator++() // prefix ++ { if (curr == c-&gt;end()) throw std::out_of_range("out of range"); ++curr; return *this ; } Checked_iter&amp; operator++(int) { Checked_iter tmp= *this; ++*this; //Checked by prefix ++ return tmp; } Checked_iter&amp; operator--() //prefix-- { if(curr == c-&gt;begin()) throw std::out_of_range("out of range"); --curr; return *this; } Checked_iter&amp; operator--(int) //postfix { Checked_iter tmp=*this; --*this; //checked by prefix -- return tmp; } typename iterator_traits&lt;Iter&gt;::difference_type index() {return curr - c-&gt;begin();} //random access only Iter unchecked() {return curr;} //+-&lt; }; template&lt;class Cont, class Iter&gt; Checked_iter&lt;Cont,Iter&gt;make_checked(Cont&amp; c, Iter i) { return Checked_iter&lt;Cont, Iter&gt;(c,i); } template&lt;class Cont&gt;Checked_iter&lt;Cont,typename Cont::iterator&gt;make_checked(Cont&amp; c) { return Checked_iter&lt;Cont, typename Cont::iterator&gt;(c,c.begin()); } void f_count(std::vector&lt;int&gt;&amp; vec) { int count=0; try{ Checked_iter&lt;std::vector&lt;int&gt;&gt; p(vec, vec.begin()); while(true){ ++p; ++count; } } catch(std::out_of_range){ std::cout&lt;&lt;"overrun after"&lt;&lt;count&lt;&lt;"tries\n"; } } void print_v(std::vector&lt;int&gt;vec) { for(auto i=vec.begin();i != vec.end(); ++i) std::cout&lt;&lt;*i&lt;&lt;" "; } void test_f(){ int a[] = {0,1,2,3,4,5,6,7,8,9}; std::vector&lt;int&gt; l(a, a+sizeof(a)/sizeof(int)); print_v(l); f_count(l); } int main() { test_f(); return 0; } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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