Thanks, it sure worked. I thought if T was int, then vector<T>::iterator iter; becomes vector<int>iterator iter, which is a type itself, considering the fact it works when the function is not written as a template.
Anyway thanks, I guess I still have a lot to understand. Lawal.O ________________________________ From: Jim Smith <[email protected]> To: [email protected] Sent: Tuesday, July 7, 2009 11:12:34 PM Subject: Re: [c-prog] problem with vector iterator bool isPresent=false; vector<T>::iterator p; In the code above the compiler does not know that vector<T>::iterator is a type unless you use typename. Example: template <typename T> bool isNumberInVector( vector<T>&v, T numToCheck) { bool isPresent=false; typedef typename vector<T>::iterator Iter; Iter p; for(p=v.begin( );p!=v.end( );p++) if((*p)==numToCheck ) isPresent=true; return isPresent; } That should work. Best Regards, Jim Smith E-mail: jmclaurin11@ yahoo.com --- On Tue, 7/7/09, wolexzo2 <wolex...@yahoo. com> wrote: From: wolexzo2 <wolex...@yahoo. com> Subject: [c-prog] problem with vector iterator To: c-p...@yahoogroups. com Date: Tuesday, July 7, 2009, 12:28 AM Below is a template of a function isNumberInVector. I get errors about the iterator when I run this program. errors such as error: expected `;' before `p' `p' was not declared in this scope error: dependent-name `std::vector: :iterator' is parsed as a non-type, but instantiation yields a type I don't get error when I simply make the function non-template and use vector<int> instead. So What could be the cause? Thanks, Lawal. O #include <iostream> #include <string> #include<cstdlib> #include<vector> using namespace std; template <typename T> bool isNumberInVector( vector<T>&v, T numToCheck) { bool isPresent=false; vector<T>::iterator p; for(p=v.begin( );p!=v.end( );p++) if((*p)==numToCheck ) isPresent=true; return isPresent; } int main(){ vector<int> vect; for(int i=0;i<20;i++ ) if(i%2==0) vect.push_back( i); int number=4; if(isNumberInVector (vect,number) )cout<<"Yes It is"; else cout<<"No it isn't"; } [Non-text portions of this message have been removed] [Non-text portions of this message have been removed]
