相关疑难解决方法(0)

为什么在此模板中使用typedef是必要的?

当我在Visual Studio 2005中编译此代码时:

  template <class T>
  class CFooVector : public std::vector<CFoo<T>>
  {
  public:
    void SetToFirst( typename std::vector<CFoo<T>>::iterator & iter );
  };

  template <class T>
  void CFooVector<T>::SetToFirst( typename std::vector<CFoo<T>>::iterator & iter )
  {
    iter = begin();
  }
Run Code Online (Sandbox Code Playgroud)

我收到这些错误:

c:\home\code\scantest\stltest1\stltest1.cpp(33) : error C2244:     'CFooVector<T>::SetToFirst' : unable to match function definition to an existing declaration
    c:\home\code\scantest\stltest1\stltest1.cpp(26) : see declaration of 'CFooVector<T>::SetToFirst'
    definition
    'void CFooVector<T>::SetToFirst(std::vector<CFoo<T>>::iterator &)'
    existing declarations
    'void CFooVector<T>::SetToFirst(std::_Vector_iterator<_Ty,_Alloc::rebind<_Ty>::other> &)'
Run Code Online (Sandbox Code Playgroud)

如果我将一个typedef添加到CFooVector模板,我可以获得编译和工作的代码:

  template <class T>
  class CFooVector : public std::vector<CFoo<T>>
  {
  public:
    typedef typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates typedef typename

3
推荐指数
1
解决办法
1185
查看次数

标签 统计

c++ ×1

templates ×1

typedef ×1

typename ×1