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

11,254

You pass int as your first template parameter T. So std::iterator_traits<T>::value_type is std::iterator_traits<int>::value_type, which is incorrect. You meant

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

Share:
11,254
rodrykbyk
Author by

rodrykbyk

Updated on June 24, 2022

Comments

  • rodrykbyk
    rodrykbyk almost 2 years

    Hello. I am trying to run the following code (just for training purposes):

    #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());
    }
    

    The function minimum is working fine and there are no issues. However, when I uncomment last line I recieve the following error:

    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>’
    

    Whats wrong with the first template then? I would be more than grateful for explanation.