-
weird compile error
Code:
template <class C, class T> int count2(const C &v, T val)
{
C::const_iterator i = find(v.begin(), v.end(), val);
int n = 0;
while(i != v.end()) {
++n;
++i;
i = find(i, v.end(), val);
}
return n;
}
This functions counts the number of occurences of 'val' inside the type C.
What i dont understand is why do i get compile error for this that i is undeclared...
-
The problem that I see is that C::const_iterator is a name dependent on the template parameter C, hence it should be disambiguated with typename otherwise it would be assumed to be a member name instead of a type name, i.e.,
Code:
typename C::const_iterator i = find(v.begin(), v.end(), val);
-
Mind you, if this isn't merely an exercise, there's a std::count that already does what you want.