2014-10-12 40 views
0

这里是我的代码:C++错误:迭代器是不是一个类型的“

template <typename container_type> 
void transfer(container_type container, iterator begin, iterator end) { 
    for (; begin != end; begin++) 
     if (!element_in_container(container, *begin)) 
      container.insert(iterator, *begin); 
} 

我得到的错误'iterator is not a type'

我试过在iterator之前加std::container_type::,没有帮助。我试着将模板定义为template <typename container_type<typename T> >,迭代器为container_type<T>::iterator,没有运气。怎么了?

+4

'typename container_type :: iterator'? – 2014-10-12 10:50:37

+1

确实,'iterator'不是一个类型。 – 2014-10-12 10:50:39

+0

可能的重复:[我在哪里以及为什么必须放置“template”和“typename”关键字?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have- to-put-the-template-and-typename-keywords) – 2014-10-12 10:53:13

回答

4

我想你指的是以下

template <typename container_type> 
void transfer(container_type container, typename container_type::iterator begin, 
             typename container_type::iterator end) { 

考虑到,在任何情况下,你的函数是错误的,因为在容器中的迭代器插入的元素后可能无效。

1

I tried adding std:: or container_type:: before iterator, didn't help.

container_type::iterator是一个从属名称,因此,你需要typename关键字之前,把它当作一种类型(typename container_type::iterator)。这在深度here解释。

相关问题