I have this class:
template <class S, class P, class A>
class Task
{
private:
timeval start;
boost::ptr_vector<S> states;
boost::ptr_vector<P> policies;
public:
P findPolicy(S *state);
S findState(S *state);
};
When I try to define findPolicy or findState using an iterator:
template <class S, class P, class A>
S Task<S,P,A>::findState(S *state)
{
boost::ptr_vector<S>::iterator it;
for ( it = policies.begin(); it < policies.end(); ++it)
{
// blah
}
}
Defined after the class, compiler says:
error: expected ';' before it;
Even trying to define the function in the class declaration gives me the same error.
I’m puzzled since this is how I’ve been using boost::ptr_vector iterators so far.
The only thing that seems to work is good old fashioned:
for (int i = 0; i < policies.size(); i++)
{
if (policies[i].getState() == state)
{
return policies[i];
}
}
What am I missing ?
This needs to use the C++ keyword
typename:Otherwise, C++ doesn’t know what
ptr_vector<S>::iteratoris supposed to be. This is because the definition ofptr_vector<S>depends on the template parameterS, and the value ofSis not known at the time of the template’s definition. But the compiler needs to be able to make sense of the lineptr_vector<S>::iteratorwithout knowing whatSis exactly.So compilers assume that dependent names are variables (so a static member of
ptr_vector<S>); you need to usetypenamein order to tell the compiler that the dependent name is a type and not a variable.