【问题标题】:vector<T>::iterator - invalid? [duplicate]vector<T>::iterator - 无效? [复制]
【发布时间】:2010-08-09 20:49:29
【问题描述】:

可能重复:
g++ “is not a type” error

以下内容无法编译:

1    template<typename T>
2    void foo(std::vector<T>::iterator & i)
3    {  
4    }

在 Visual Studio 上,我收到以下错误:

>(2) error C2065: 'i' : undeclared identifier
>(4) warning C4346: 'std::vector<_Tp>::iterator' : dependent name is not a type
     prefix with 'typename' to indicate a type
>(4) error C2182: 'foo' : illegal use of type 'void'
>(4) error C2998: 'int foo' : cannot be a template definition

【问题讨论】:

  • 找到这个duplicate,还有更多但我找不到它们。 :S

标签: c++ templates vector


【解决方案1】:

std::vector&lt;T&gt;::iterator 是一个依赖模板参数的类型,即T。因此,您应该使用它作为前缀typename:

template<typename T>
void foo(typename std::vector<T>::iterator & i)
{  
}

Here's an explanation.

【讨论】:

  • @Fred:我已经达到极限了,所以我正在快速寻找可接受的答案。 :P
  • 哈哈,那我明天接受你的回答:)(这样你就可以得到+15)
  • @Jacob:接受的答案不受限制:stackoverflow.com/faq
猜你喜欢
  • 1970-01-01
  • 2013-02-18
  • 2013-10-25
  • 2022-11-21
  • 2016-11-26
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多