Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Using any iterator type in a faux-STL class?
    primarykey
    data
    text
    <p>I'm creating a container class very similar to a vector (class project), and have defined an iterator subclass. One of the ctors takes two iterator arguments, and builds the data structure (doubly linked list) using the half-open range they represent. </p> <p>The code below (almost) works for cases when insert(iterator, iterator) is called with two iterators of type Sol::iterator (for some reason, *ita always points to the right values, but the insert(*ita) call doesn't seem to add the value??). The (larger) problem is that the insert call needs to work for all iterator types (ex. vector::iterator), and I haven't been able to track down how to make that work. I've tried typedef / typename T::iterator iterator, but the quietest g++ has been was with 'typename T::iterator iterator', and it returns </p> <pre><code>g++ ex1.cc -o sol -Wall -Wextra -ansi -pedantic In file included from ex1.cc:1:0: Sol.h:87:16: error: expected ‘)’ before ‘ita’ ex1.cc:109:5: error: expected ‘}’ at end of input In file included from ex1.cc:1:0: Sol.h:84:3: error: expected unqualified-id at end of input make: *** [ex1] Error 1 </code></pre> <p>which doesn't make much sense; at least to me. Here's the broad strokes:</p> <pre><code>template &lt;class T, int find_val = 1, class Compare = std::less&lt;T&gt; &gt; class Sol{ public: typedef unsigned int size_type; //typedef'ing class variable types typedef T key_type; typedef T value_type; //typename T::iterator iter; private: struct node{ //Used as 'links' in the chain that is the doubly linked list T data; node *prev; node *next; }; node *head, *tail; //The head and tail of the dll int find_promote_value; //How much should find() shift a value? public: class iterator{ private: node *no; friend class Sol; iterator(node *p) : no(p){ } public: iterator() : no(0){ } iterator operator++(){ //Preincrement no = no-&gt;next; return *this; } iterator operator++(int){ //Postincrement iterator temp = *this; ++*this; return temp; } iterator operator--(){ //Predecrement no = no-&gt;prev; return *this; } iterator operator--(int){ //Postdecriment iterator temp = *this; --*this; return temp; } T operator*(){ return no-&gt;data; } T *operator-&gt;(){ return &amp;no-&gt;data; } bool operator==(const iterator &amp;rhs){ return no == rhs.no; } bool operator!=(const iterator &amp;rhs){ return no != rhs.no; } }; Sol() : head(0), tail(0), find_promote_value(find_val){ } Sol(const Sol &amp;rhs) : head(rhs.head), tail(rhs.tail), find_promote_value(rhs.find_promote_value){ } typename T::iterator iterator; Sol(iterator ita, iterator itb){ //Sol.h::87. Problem line while (ita != itb){ iterator itr = insert(*ita); std::cout &lt;&lt; "Inserting " &lt;&lt; *ita &lt;&lt; ",printout: " &lt;&lt; *itr &lt;&lt;"\n"; //dump(); ++ita; } } ~Sol(){ clear(); } iterator insert(T input){ node *n = new node; n-&gt;next = NULL; n-&gt;prev = tail; n-&gt;data = input; if(!tail){ head = n; tail = n; } else{ tail-&gt;next = n; tail = n; } return iterator(tail); } }; </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.
 

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