Template method

Hello everyone, I have a class where a method based on the type passed I should return a value. Can you tell me where I'm wrong.

prototype declared in the header file:
 
template <typename T>int getNum() const;


Code of the cpp file:
1
2
3
4
5
6
template <typename T> int class::getNum() const{
    int c = 0;
    for(int i=0;i<v.size();i++)
        if(typeid(*(Pro*)v.at(i)) == typeid(T)) c++;
    return c;
}


to invoke the method as I do:
1
2
3
ostream & operator << (ostream & os, Pro & obj) {
     return os << obj.getNum();
}


thank you in advance. ;)
Templates must be defined inline in the header file, otherwise the compiler cannot see the definition to instantiate the template with.
you wrote typeid(T)

i dont think that is possible because "T" is just a placeholder for a variable type
for eg:
template <class T>
T add(T a1, T a2)
{
return a1+a2;
}

where a1 and a2 can be a int, double, char, float or whatever variable type.

so basically typeid(T) is just passing a variable type T
@geosnipes: there is nothing at all wrong with typeid(T)
> typeid(*(Pro*)v.at(i))
¿why are you casting?

> a method based on the type passed
> return os << obj.getNum();
¿where are you passing the type?
Topic archived. No new replies allowed.