C++ boost :: ptr_vector <S> :: iterator问题

Æle*_*lex 3 c++ boost-iterators

我有这门课:

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);

};
Run Code Online (Sandbox Code Playgroud)

当我尝试使用迭代器定义findPolicy或findState时:

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 
  }
}
Run Code Online (Sandbox Code Playgroud)

在类之后定义,编译器说:

error: expected ';' before it;
Run Code Online (Sandbox Code Playgroud)

即使尝试在类声明中定义函数也会给出同样的错误.我很困惑,因为到目前为止我一直在使用boost :: ptr_vector迭代器.似乎唯一有用的东西是老式的:

for (int i = 0; i < policies.size(); i++)
  {
    if (policies[i].getState() == state)
    {
     return policies[i];
    }
  }
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Nic*_*las 7

  boost::ptr_vector<S>::iterator it;
Run Code Online (Sandbox Code Playgroud)

这需要使用C++关键字typename:

typename boost::ptr_vector<S>::iterator it;
Run Code Online (Sandbox Code Playgroud)

否则,C++不知道ptr_vector<S>::iterator应该是什么.这是因为定义ptr_vector<S>取决于模板参数S,并且S在模板定义时不知道值.但是编译器需要能够理解这条线ptr_vector<S>::iterator而不知道究竟S是什么.

所以编译器假设依赖名称是变量(所以是静态成员ptr_vector<S>); 您需要使用typename以告诉编译器依赖名称是类型而不是变量.