你好。我正在尝试运行以下代码(仅用于培训目的):
#include<iostream>
#include <list>
template<class T,
template<class ,class=std::allocator<T> >class kont >
typename std::iterator_traits<T>::value_type foo_test(typename kont<T>::iterator b){return *b;}
template <class Iter>
typename std::iterator_traits<Iter>::value_type minimum(Iter b, Iter e)
{
Iter m = b;
/*
CODE
*/
return *m;
}
int main(void){
std::list<int> x;
x.push_back(10);
x.push_back(100);
std::cout <<minimum(x.begin(),x.end());
//std::cout <<foo_test<int,std::list>(x.begin());
}
最小功能运行良好,没有任何问题。但是,当我取消注释最后一行时,我收到以下错误:
main.cpp:33:50: error: no matching function for call to ‘foo_test(std::__cxx11::list<int>::iterator)’
std::cout <<foo_test<int,std::list>(x.begin());
main.cpp:7:46: note: template argument deduction/substitution failed:
main.cpp:33:50: required from here
main.cpp:7:46: error: no type named ‘value_type’ in ‘struct std::iterator_traits<int>’
那么第一个模板有什么问题呢?非常感谢您的解释。
最佳答案
你通过了int
作为你的第一个模板参数 T
.所以std::iterator_traits<T>::value_type
是std::iterator_traits<int>::value_type
,这是不正确的。你的意思是
typename std::iterator_traits<typename kont<T>::iterator>::value_type
.
关于C++ 在 ‘value_type’ 中没有名为 ‘struct std::iterator_traits<int>' 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443934/