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: [email protected]


         

         
--- On Tue, 7/7/09, wolexzo2 <[email protected]> wrote:

From: wolexzo2 <[email protected]>
Subject: [c-prog] problem with vector iterator
To: [email protected]
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]

Reply via email to