main() {
vector<int> v;
vector<int>::iterator it;
my_reverse_iterator p(it);
}
In this case the problem is clear, you're trying to create an object of a type that's not complete. You need to first create the type by instantiating the template (using template args), only then you can create an object of this type.
main() {
vector<int> v;
vector<int>::iterator it;
my_reverse_iterator <vector<int> > p(it);
}
In this case you are getting errors because you've not copied the complete implementation of reverse_iterator. :).
To explain the error
1. you need to include the file that has iterator_traits defined.
2. your c'tor takes iterator_type you're giving it iterator_type* .
2. once you correct this you should get more errors.
Basic problem is you've copied the code of something that is meant to a compiler independent, container independent, extensible (in terms of relations between different iterator classes) implementation. You need to copy a lot more to make it work.Suggestion:
Forget/dump what the "really good fundoo STL developers" have implemented. Start from scratch and implement exactly and only what you need.
Never copy code that you don't understand. Instead readup and try to write yourself, once you get stuck in some specific problem refer to the code you would've wanted to copy, to see how they solved that problem.
If you have already implemented iterator you would know the simple fact of life abt iterators is that it's a class hiding a pointer.
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
you are getting the error here.
my_reverse_iterator p(it);
this is because my_reverse_iterator is a template class; you are not using it as such.
change that to
my_reverse_iterator<vector<int>::iterator> p(it);
and you can get started on the rest of the class.
i presume you are not allowed to use the iterator adapter provided by libstdc++; if you are you could get your reverse iterator by a mere typedef. for example:
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std ;
int main()
{
char cstr[] = "abcdefghijkl" ;
typedef char* iterator ;
iterator begin(cstr), end(cstr+sizeof(cstr)-1) ; // exclude the null
copy( begin, end, ostream_iterator<char>(cout) ) ; cout << '\n' ;
typedef reverse_iterator<iterator> rev_iterator ;
rev_iterator rbegin(end), rend(begin) ;
copy( rbegin, rend, ostream_iterator<char>(cout) ) ; cout << '\n' ;
typedef reverse_iterator<rev_iterator> rev_rev_iterator ;
rev_rev_iterator rrbegin(rend), rrend(rbegin) ;
copy( rrbegin, rrend, ostream_iterator<char>(cout) ) ; cout << '\n' ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Making the changes suggested gave me the errors from before:
Anachronism: using iterator_traits as a template without a declaration.
ERROR: iterator_traits is not defined.
In researching this error, i found that i need to define it properly. There are two ways. "First, define your iterator so that it has nested types I::value_type, I::difference_type, and so on. Second, you can explicitly specialize iterator_traits for your type." - SGI.com
i tested the code (with the change my_reverse_iterator< vector::iterator > p(it) ; ) on vc++ 8, gcc 3.4.6 and icc 9.1; in all of them it compiles without errors or warnings.
vector::iterator is fully std library compliant; it works correctly with iterator_traits<>.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Seems I need to add the typedefs that didn't seem to inheret when i did it. Is this correct or does it work as a fluke?
this is correct; you need to have the typedefs for the iterator to work with iterator_traits<>. (specializing iterator_traits<> for your iterator is another way.) in a conformant implementation of libstdc++, you should have been able to inherit these typedefs from the iterator base class.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287