I am getting compilation error , please help i am not able to resolve it
class A {
public :
A() {}
virtual ~A() {}
int getID() { return m_id; }
void setID(int id) { m_id = id ; }
private:
int m_id ;
};
class B : public A {
};
class C: public A {
};
template<class T>
T* returnObject(vector<T*>& vec, int id)
{
for(vector<T*>::iterator itr =vec.begin(); itr != vec.end() ; ++itr)
{
if(id == (*itr)->getID()) return (*itr);
}
return NULL;
}
int main()
{
vector<B*> b1;
vector<C*> c1;
B *b = new B();
b->setID(10);
b1.push_back(b);
b = new B();
b->setID(20);
b1.push_back(b);
b = new B();
b->setID(30);
b1.push_back(b);
C *c = new C();
c->setID(6);
c1.push_back(c);
c = new C();
c->setID(12);
c1.push_back(c);
c = new C();
c->setID(18);
c1.push_back(c);
B *bcd = returnObject<B>(b1,30);
cout<< bcd <<endl ;
return 0;
}
I am getting compilation error
castEx.cpp: In function `T* returnObject(std::vector<T*, std::allocator<T*> >&, int)':
castEx.cpp:29: error: expected `;' before "itr"
castEx.cpp:29: error: `itr' was not declared in this scope
castEx.cpp: In function `T* returnObject(std::vector<T*, std::allocator<T*> >&, int) [with T = B]':
castEx.cpp:61: instantiated from here
castEx.cpp:29: error: dependent-name ` std::vector<T*,std::allocator<T*> >::iterator' is parsed as a non-type, but instantiation yields a type