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";
}