Note that there are some explanatory texts on larger screens.

plurals
  1. POChecked iterator not working
    primarykey
    data
    text
    <p>I'm trying to run the checked iterator example from the c++ programming language (3rd edition) book, but I'm getting errors, and I think I need some help understanding, why I'm getting these errors, I'm getting the code from the book, thanks in advance.</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;iterator&gt; #include &lt;exception&gt; #include &lt;cstddef&gt; // using namespace std; 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 std::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, 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);} //Error:Unkown type name "reference" reference operator*() { if (curr == c-&gt;end()) throw std::out_of_range("out of range"); return *curr; } //Error:Unkown type name "pointer" pointer operator-&gt;() { return &amp;*curr; // checked by * } //Error:Unkown type name "difference_type" Checked_iter operator+(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 ); } //Error:Unkown type name "difference_type" reference operator[](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; } //Error:Unkown type name "difference_type" difference_type index() {return curr-c.begin();} //random access only Iterator 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(vector&lt;int&gt;&amp; vec) { int count=0; try{ Checked_iter&lt;vector&lt;int&gt;&gt; p(vec, vec.begin()); while(true){ ++p; ++count; } } catch(out_of_range){ cout&lt;&lt;"overrun after"&lt;&lt;count&lt;&lt;"tries\n"; } } void test_f(){ int a[] = {0,1,2,3,4,5,6,7,8,9}; vector&lt;int&gt; l(a, a+sizeof(a)/sizeof(int)); print_v(l); f_count(l); } void print_v(vector&lt;int&gt;vec) { for(aut i=vec.begin();i != vec.end(); ++i) cout&lt;&lt;*i&lt;&lt;" "; } int main() { test_f(); return 0; } </code></pre> <p>I'm getting <code>unknown type name</code> error for <code>reference</code>, <code>pointer</code> and <code>difference_type</code>.</p>
    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.
    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