2012-11-12 19 views
7

जब मैं std::distance का उपयोग जीसीसी 4.7 के तहत कस्टम इटरेटर के साथ करने का प्रयास करता हूं, तो यह difference_type नहीं ढूंढने की शिकायत करता है। मुझे दुख की बात नहीं है कि यह क्यों विफल रहता है।अंतर_ प्रकार नहीं मिला

/usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:114:5: error: no type named ‘difference_type’ in ‘struct std::iterator_traits<nit>’

+0

यहां समाधान हो सकता है: http://www.cplusplus.com/forum/general/11428/। –

+1

['std :: iterator'] (http://en.cppreference.com/w/cpp/iterator/iterator) के उदाहरण से अपनी कक्षा को विरासत में लाने का प्रयास करें। मैं कल्पना कर सकता हूं कि 'std :: iterator_traits' केवल उन चीज़ों के लिए विशिष्ट है जो उस से प्राप्त होते हैं। –

+0

इसके आगे, मैं 'कक्षा नाइट: सार्वजनिक std :: iterator 'के साथ आगे बढ़ सकता हूं, लेकिन' टी' को एक गैर-शून्य प्रकार होना चाहिए, और आपको एक प्रदान करना होगा काम करने के लिए 'ऑपरेटर'। वैकल्पिक रूप से आपके पास 'bidirectional_iterator_tag' हो सकता है, लेकिन फिर आपको incrementors और comparators प्रदान करने की आवश्यकता है। –

उत्तर

3

आप सभी आवश्यक प्रकार/ऑपरेटरों को परिभाषित करने की कोशिश की है:

#include <iterator> 

class nit { 
public: 
    typedef int difference_type; 
}; 

int main() { 
    const nit test1; 
    std::distance(test1, test1); 
    return 0; 
} 

त्रुटि देता है?

#include <iterator> 

struct nit 
{ 
    typedef std::random_access_iterator_tag iterator_category; 
    typedef int value_type; 
    typedef int difference_type; 
    typedef int* pointer; 
    typedef int& reference; 

    bool operator==(nit const&) 
    { 
    return true; 
    } 

    bool operator!=(nit const&) 
    { 
    return false; 
    } 

    int operator-(nit const&) 
    { 
    return 0; 
    } 

    nit() 
    { 
    } 
}; 

int main() 
{ 
    nit const test1; 
    std::distance(test1, test1); 

    return 0; 
} 
0

या तो, आप (के साथ या std :: iterator की मदद के बिना) है कि std अपनी कक्षा में :: iterator_traits उम्मीद कर रही है सब typedefs प्रदान करने के लिए है या आप विशेषज्ञ std :: खुद iterator_traits है।

This version जीसीसी के अन्य त्रुटि संदेश उत्सर्जित करता है लेकिन यह इस तथ्य को नहीं बदलेगा कि आपका कोड अवैध है।

prog.cpp: In function ‘int main()’: 
prog.cpp:9: error: uninitialized const ‘test1’ 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++v4/bits/stl_iterator_base_types.h: At global scope: 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<nit>’: 
prog.cpp:10: instantiated from here 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:133: error: no type named ‘iterator_category’ in ‘class nit’ 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:134: error: no type named ‘value_type’ in ‘class nit’ 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:136: error: no type named ‘pointer’ in ‘class nit’ 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:137: error: no type named ‘reference’ in ‘class nit’ 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_funcs.h: In function ‘typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = nit]’: 
prog.cpp:10: instantiated from here 
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_funcs.h:119: error: no matching function for call to ‘__iterator_category(nit&)’ 
संबंधित मुद्दे