简体   繁体   English

c ++模板和迭代器

[英]c++ template and iterator

I am trying to declare this function : 我想宣布这个功能:

template <typename T> void hi(std::map<T,double> m,std::vector<std::map<T,double>::iterator > vec)
{
    //....
}

I want the function to have as parameters a map of and a vector of iterators on this map. 我希望函数在此映射上具有迭代器的映射和向量作为参数。 But the g++ compiler does not seem to like it and I receive the following error : 但g ++编译器似乎不喜欢它,我收到以下错误:

 type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp,     class _Alloc> class std::vector’
: error:   expected a type, got ‘std::map::iterator’
: error: template argument 2 is invalid

Any help please? 有什么帮助吗?

You must write typename before the iterator argument 您必须在迭代器参数之前写入typename

template <typename T> 
void hi(std::map<T,double> m, std::vector<typename  std::map<T,double>::iterator > vec)
                                          ^^^^^^^^
{
    //....
}

Yes, it is unfortunate that typename has 2 different meanings. 是的,不幸的是, typename有两种不同的含义。 Some authors use template<class T> for precisely that reason, in order to visually disambiguate template parameter declarations from dependent type extraction. 一些作者正是出于这个原因使用template<class T> ,以便在视觉上消除模板参数声明与依赖类型提取的歧义。 Especially in template-metaprogramming this can help to make code a bit more readable. 特别是在模板元编程中,这有助于使代码更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM