2010-07-01 80 views
5

我試圖編譯一個反向迭代器,但我嘗試這樣做給一個可怕的爛攤子。代碼的小例子,是...反向迭代器將無法編譯

#include <iostream> 
#include <vector> 
#include <algorithm> 

class frag { 
    public: 
     void print (void) const; 
    private: 
     std::vector<int> a; 
}; 

void frag::print (void) const 
{ 
    for (std::vector<int>::reverse_iterator iter = a.begin(); 
     iter         != a.end(); 
     ++iter) { 
     std::cout << *iter << std::endl; 
    } 
} 

,並試圖編譯它產生以下...

In file included from /usr/include/c++/4.4/bits/stl_algobase.h:69, 
      from /usr/include/c++/4.4/bits/char_traits.h:41, 
      from /usr/include/c++/4.4/ios:41, 
      from /usr/include/c++/4.4/ostream:40, 
      from /usr/include/c++/4.4/iostream:40, 
      from frag.cpp:1: 
/usr/include/c++/4.4/bits/stl_iterator.h: In constructor ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&) [with _Iter = __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >]’: 
frag.cpp:14: instantiated from here 
/usr/include/c++/4.4/bits/stl_iterator.h:134: error: no matching function for call to ‘__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(__gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >)’ 
/usr/include/c++/4.4/bits/stl_iterator.h:686: note: candidates are: __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator(const _Iterator&) [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >] 
/usr/include/c++/4.4/bits/stl_iterator.h:683: note:     __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator() [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >] 
/usr/include/c++/4.4/bits/stl_iterator.h:669: note:     __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&) 

昨天有關於這個問題的一個問題,但我不認爲這與它不是模板相同。如果一個向量在本地以類似的方式聲明,那麼它非常高興。 (Ubuntu 10.4上的g ++)。

任何人都知道我應該怎麼辦呢?

回答

18

您需要使用const_reverse_iteratorprintconst功能,所以aconst)和a.rbegin()a.rend()而不是begin()end()

+0

dang,幾分鐘後更快:( – rubenvb 2010-07-01 14:57:10

+0

@rubenvb:儘管我的第一次嘗試只發現了一個問題,但瘋狂地編輯以更新第二個問題 – 2010-07-01 14:58:45

+0

:D有些問題似乎吸引了衆人,不是嗎? – rubenvb 2010-07-01 15:05:02

2

你必須一個反向迭代器分配給一個反向迭代。與比較相同。

for (std::vector<int>::reverse_iterator iter = a.rbegin(); iter != a.rend(); ++iter); 

Edit(對於完整性) 正如其他人所注意到的,的const_reverse_iterator會在這裏也必須:反向迭代器使用方法rbeginrend獲得。

+0

的問題,我並沒有真正在現實犯了那個錯誤程序,但我在最小的例子中嘗試了很多東西。 – 2010-07-01 15:20:10

3

兩件事情,可能在你的代碼會導致一個問題:

  1. print()函數聲明const,所以你(可能需要或應該)使用const_reverse_iterator

  2. 您正在創建從normal_iteratorstd::vector<T>::begin())一reverse_iterator

+0

在第二點中問題不指向最後一個元素,但分配和比較不兼容的類型'normal_iterator'和'reverse_iterator' – 2010-07-01 15:04:28

+0

通過編輯修正。謝謝 – rubenvb 2010-07-01 15:17:30

1

以上答案已經指出,一個const_reverse_iteratorrbegin/rend需要在這種情況下。

除此之外,一個好的做法是使用crbegincrend(介紹C++ 11)明確表示您所請求的迭代器的const版本,因爲print功能是常量。