2012-11-12 39 views
7

Khi tôi cố gắng sử dụng std::distance với trình biến đổi tùy chỉnh trong gcc 4.7, nó than phiền về việc không tìm kiếm difference_type. Tôi buồn bã không biết tại sao nó thất bại.differ_type không được tìm thấy

#include <iterator> 

class nit { 
public: 
    typedef int difference_type; 
}; 

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

cung cấp cho các lỗi:

/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

Đây có thể là giải pháp: http://www.cplusplus.com/forum/general/11428/. –

+1

Thử kế thừa lớp của bạn từ một thể hiện của ['std :: iterator'] (http://en.cppreference.com/w/cpp/iterator/iterator). Tôi có thể tưởng tượng rằng 'std :: iterator_traits' chỉ dành riêng cho những thứ thừa kế từ đó. –

+0

Hơn nữa, tôi có thể tiếp tục với 'class nit: public std :: iterator ', nhưng 'T' cần phải là một loại không trống, và bạn cần cung cấp 'toán tử-' để làm việc này. Ngoài ra, bạn có thể có 'bidirectional_iterator_tag', nhưng sau đó bạn cần phải cung cấp các bộ gia tăng và so sánh. –

Trả lời

3

Bạn đã cố gắng xác định tất cả các yêu cầu loại/nhà khai thác?

#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

Hoặc, bạn phải cung cấp tất cả các typedefs (có hoặc không có sự giúp đỡ của std :: iterator) trong lớp học của bạn mà std :: iterator_traits được mong đợi hoặc bạn phải chuyên std :: iterator_traits mình.

This version của GCC phát ra các thông báo lỗi khác nhưng không thay đổi thực tế là mã của bạn là bất hợp pháp.

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&)’ 
Các vấn đề liên quan