Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;boost::ptr_向量<;S>;::迭代器问题_C++_Boost Iterators - Fatal编程技术网

C++ C++;boost::ptr_向量<;S>;::迭代器问题

C++ C++;boost::ptr_向量<;S>;::迭代器问题,c++,boost-iterators,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); }; 即使尝试

我有这门课:

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

};
即使尝试在类声明中定义函数,也会出现同样的错误。 我很困惑,因为到目前为止我一直在使用boost::ptr_向量迭代器。 唯一有效的方法是老式的:

for (int i = 0; i < policies.size(); i++)
  {
    if (policies[i].getState() == state)
    {
     return policies[i];
    }
  }
for(int i=0;i
我错过了什么

  boost::ptr_vector<S>::iterator it;

否则C++不知道什么是代码> pTrIVAuth::迭代器< /代码>应该是。这是因为

ptr\u向量的定义取决于模板参数
S
,而
S
的值在模板定义时是未知的。但是编译器需要能够理解行
ptr_vector::iterator
,而不知道
S
到底是什么


因此编译器假定依赖名是变量(因此是
ptr_vector
的静态成员);您需要使用
typename
来告诉编译器依赖名称是一个类型而不是一个变量。

它没有解释错误消息IMO,但是在您发布的代码中,您分配了一个
boost::ptr_vector::iterator
变量和一个
boost::ptr_vector

::iterator

值。

我认为您需要添加typename-我认为您需要:

typename boost::ptr_vector<S>::iterator it;

这是因为
It
前面的类型未被编译器视为类型,因此需要添加
typename

在发布错误时,最好指出错误出现的行。不要使用“Itboost::ptr_vector
依赖于模板参数
S
,因此编译器无法知道它是类型还是值。当出现这种歧义时,它会假定它是值,从而导致编译错误。使用
typename
关键字限定声明会告诉编译器将其视为依赖类型。@Alex-在
findState
中,当
策略
向量

时,使用
向量::迭代器。当然,这取决于实际的S和P,所以有时也可以。
  boost::ptr_vector<S>::iterator it;
typename boost::ptr_vector<S>::iterator it;
typename boost::ptr_vector<S>::iterator it;
error: expected ';' before it;