C++ 在"struct std::iterator_traits"中没有名为"value_type"的类型<int>

C++ no type named ‘value_type’ in ‘struct std::iterator_traits<int>'

本文关键字:value type 类型 lt gt int std struct iterator traits C++      更新时间:2023-10-16

你好。我正在尝试运行以下代码(仅出于培训目的(:

#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_typestd::iterator_traits<int>::value_type,这是不正确的。你的意思是

typename std::iterator_traits<typename kont<T>::iterator>::value_type

相关文章: