I am a new C ++ programmer, I learned Java and ANSI C some time ago and decided to give it a chance.
Well, I love C ++, but I didn't like how iterators work:
In java, you can make the entire container private and implement the getter function for iterator, and the iterator has a hasNext() method that returns a boolean depending on whether it reaches the end of the container.
The only way I found something similar in C ++ is to write 2 getters, iteratorBegin() and iteratorEnd() , which return the internator corresponding to the first and last positions, increasing the iterator returned by iteratorBegin() , and comparing it with iteratorEnd() , allowed me to iterate over the container until reaching the end position
But I want to use only one getter method, and I thought: “Let me make my own iterator class”
So far, it’s so good that I have successfully dealt with sets and lists, but I can’t do it with maps, here is the code that bothers me: (the class is defined in a separate .h, this is called customIterator.cpp)
template<typename T, typename D> const D& custIterator<T,D>::next() { const D& obj = (*it); if(hasNext()) { it++; } return obj; }
when compiling a specialized method, he says: error: "the map was not declared in this area although I added #include <map> on top of the file
I am using gcc version 4.4.5 (Debian 4.4.5-8) with code blocks
Please, I need some help.
Thanks for attention!
c ++ iterator templates map partial-specialization
Imanol barba sabariego
source share