   problems with std::list::iterator
  I am trying to write a function to retrieve a piece of data from a list, using templates, but I'm getting a compiler error:template<class T>T& ListDator<T>::operator [](int pos){std::list<T>::iterator it = values.begin();//expected ';' before 'it'for(unsigned int i=0; i<pos; i++){it++;//'it' was not declared in this scope}return *it;//'it' was not declared in this scope}Can anyone tell me what's causing this?   
  Instd::list<T>::iterator it = values.begin();//expected ';' before 'it'the term std::list<T>::iterator is a dependent name.The quick solution is to add a typename before that statement.The better solution is to learn about dependent names in C++:0) IBM on topic1) or better, if you target serious template programming, get "C++ Templates: The Complete Guide" by Vandervoorde/Josuttis;  Which compiler are you using?When in doubt with templates and types, just add typename to the front of the offending line ;) (that is, try typename std::list<T>::iterator it = values.begin();)[edit]Ninjad!   ;Quote:Original post by HodgmanWhich compiler are you using?When in doubt with templates and types, just add typename to the front of the offending line ;) (that is, try typename std::list<T>::iterator it = values.begin();)I guess it is not g++, as it will give a helpful error message (approximately "use typename to disambiguate" or so).Quote:[edit]Ninjad!Yay, ninja :D   ;  Two things I noticed about your code:1. Always prefer ++it to it++ if you're not interested in the value of the expression.2. std::advance already exists ;-)   ;Quote:Original post by DevFred1. Always prefer ++it to it++ if you're not interested in the value of the expression.why?<< actually curious not judgmental.;Quote:Original post by way2lazy2careQuote:Original post by DevFred1. Always prefer ++it to it++ if you're not interested in the value of the expression.why?<< actually curious not judgmental.it++ can be slower than ++it.  More often in theory than in practice thanks to the wonderful world of optimizing compilers, but it can't hurt to prefer ++it.   ;Quote:Original post by MaulingMonkeyit++ can be slower than ++it.  More often in theory than in practice thanks to the wonderful world of optimizing compilers, but it can't hurt to prefer ++it.To expand on why slightly, post-increment operator++ has to return the value of it before the increment happens (So it2 = it++; and similar constructs work).Because of that, the post-increment operator++ is often implemented as:Foo operator++(){   Foo tmp(*this);   ++(*this);   return tmp;}Which means a temporary object is constructed and returned. The optimisation is usually theoretical because a good compiler should be able to see that the temporary object isn't used, and constructing it has no side effect, so should generate code identical to pre-increment operator++. In theory.   ;  Is there a reason you aren't using a random access container?   