Try using typedef typename here, vector<T>::iterator to declare iterator p like this:
typedef typename vector<T>::iterator Iter; Iter p; .... typename is used to let the compiler know that vector<T>::iterator is a type. Best Regards, Jim Smith E-mail: [email protected] --- On Tue, 7/7/09, Olufowobi Lawal <[email protected]> wrote: From: Olufowobi Lawal <[email protected]> Subject: Re: [c-prog] problem with vector iterator To: [email protected] Date: Tuesday, July 7, 2009, 5:50 PM Could anyone help with this? 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]
