2011-11-17 3 views
1

Stroustrup의 C++ 책에서 "checked iterator"의 예제를 테스트하려고합니다. 코드에 런타임 오류가 있습니다. "목록 반복자가 호환되지 않습니다."iterator를 체크했습니다

오류는 "valid()"기능의 "if(c->end() == p)"에 의해 트리거됩니다. 이 오류는 무엇을 의미합니까? c->end()p은 모두 list<int>::iterator이어야합니다.

또한 왜 "연산자"가 그런 식으로 구현되었는지 이해할 수 없습니다. BTW, VS2008 사용하고 있습니다.

struct out_of_bounds{ 
    out_of_bounds() {} 
}; 

template<class Cont, class Iter = typename Cont::iterator> 
class Checked_iter : public iterator_traits<Iter> // deriving from iterator_traits 
{ 
    Iter curr; // iterator for current position 
    Cont* c; // pointer to current container 
    // ... 
public: 

    void valid(Iter p){ 
     if (c->end() == p) 
      return; 
     for (Iter pp = c->begin(); pp != c->end(); ++pp){ 
      if (pp == p) 
       return; 
     } 
     throw out_of_bounds(); 
    } 

    friend bool operator==(const Checked_iter& i, const Checked_iter& j){ 
     return i.c == j.c && i.curr == j.curr; 
    } 

    // no default initializer 
    // use default copy constructor and copy assignment 
    Checked_iter(Cont x, Iter p) : c(&x), curr(p) { valid(p); } 

    reference operator*(){ 
     if (curr == c->end()) throw out_of_bounds(); 
     return *curr; 
    } 

    pointer operator->(){ 
     return &*curr; // checked by * 
    } 

    Checked_iter operator+(difference_type d){ // for random-access iterator only 
     if (c->end() - curr <= d ) 
      throw out_of_bounds(); 
     return Checked_iter(c, curr+d); 
    } 

    reference operator[](difference_type d){ // for random-access iterator only 
     if (c->end() - curr <= d ) throw out_of_bounds(); 
     return c[d]; 
    } 

    Checked_iter& operator++(){ // prefix ++ 
     if (curr == c->end()) throw out_of_bounds(); 
     ++curr; 
     return *this; 
    } 

    Checked_iter& operator++(int) { // postfix ++ 
     Checked_iter temp = *this; 
     ++*this; // checked by prefix ++ 
     return temp; 
    } 

    Checked_iter& operator--() { // prefix -- 
     if (curr == c->begin()) throw out_of_bounds(); 
     --curr; 
     return *this; 
    } 

    Checked_iter& operator--(int) { // postfix -- 
     Checked_iter temp = *this; 
     --*this; // check by prefix 
     return temp; 
    } 

    difference_type index() { return curr-c.begin(); } // random-access only 
    Iter unchecked(){ return curr; } 
}; 

void f2(list<int>& ls){ 

    int count = 0; 
    try{ 
     Checked_iter< list<int> > p(ls, ls.begin()); 
     while(true){ 
      ++p; 
      ++count; 
      //cout << "element: " << *p << " count: " << count << endl; 
     } 
    } 
    catch(out_of_bounds){ 
     cout << "overrun after " << count << " tries \n"; 
    } 

} 

void test9(){ 
    int a[] = {0,1,2,3,4,5,6,7,8,9}; 
    list<int> l(a, a+sizeof(a)/sizeof(int)); 
    show(l); 
    f2(l); 
} 
+0

아, 그래, 내가 뭘 생각했는지 모르겠다. 댓글이 삭제되었습니다. –

+0

코드의 변수 이름이 약간 혼란 스럽습니다. f()의 p는 valid()의 p와 같지 않습니다. 그러나 나는 그것이 중요하지 않다고 생각한다. – ubbdd

답변

3

Checked_iter의 생성자는 계속 &하지 계속을해야합니다. 임시 복사본에 대한 포인터를 저장하고 있습니다.

+0

그게 전부 야. 그것은 작동합니다. 고마워요! – ubbdd

관련 문제