在类模板的方法中实例化模板类型

时间:2015-08-14 17:59:40

标签: c++ templates

我在实例化迭代器的行上遇到错误。有什么想法吗?如果我用显式类型(例如int)替换模板参数(T),问题就会消失:

template <typename T>
class StateFilter
{
  vector<T> state_;
  vector<T> filter_;

 public:
  StateFilter() {}

  T calc()
  {
    T sum = 0;
    //T* state_it = state_.begin();
    //T* filter_it = filter_.begin();
    vector<T>::iterator state_it = state_.begin();
    vector<T>::iterator filter_it = filter_.begin();
    while(filter_it < filter_.end())
    {   
      sum += (*state_it++) * (*filter_it++);
    }   
    return sum;
  }
};

错误:

rcomp.cc: In member function ‘T StateFilter<T>::calc()’:
rcomp.cc:278: error: expected `;' before ‘state_it’
rcomp.cc:279: error: expected `;' before ‘filter_it’
rcomp.cc:280: error: ‘filter_it’ was not declared in this scope
rcomp.cc:282: error: ‘state_it’ was not declared in this scope

0 个答案:

没有答案