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

标签 c++ c++11 templates iterator

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

#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 .

关于C++ 在 ‘value_type’ 中没有名为 ‘struct std::iterator_traits<int>' 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443934/

相关文章:

css - 当我点击一个链接时,如何使这个菜单在 Wordpress 上消失?

c++ - 在 ADL 期间如何防止遮盖?

c++ - 最多 3 个值,左关联版本与右关联版本的性能

c++ - 修改对内部数据的引用时更新 STL 优先级队列

c++ - 适用于 Windows 的良好 c/c++ 编译器

c++ - 如何使用 initializer_list

c++ - 模板<int v>类Foo的父类(super class)

c++ - 在 HtmlHelp 调用中使用 HelpId

c++ - std::result_of 不编译

c++ - 为什么 std::is_same 不起作用?