2017-04-17 5 views
2

こんにちは。私は次のコードを実行しようとしています(訓練目的のためだけです):'struct std :: iterator_traits <int>'に 'value_type'という名前のC++型がありません

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

最初のテンプレートで何が間違っていますか?私は説明に感謝する以上のものです。

答えて

1

最初のテンプレートパラメータTとしてintを渡します。したがってstd::iterator_traits<T>::value_typestd::iterator_traits<int>::value_typeですが、これは間違っています。つまり、

typename std::iterator_traits<typename kont<T>::iterator>::value_typeです。

+2

ありがとうございます。時には、最も明白なことは不明です。 – rodrykbyk

関連する問題