2010-02-01 56 views
2

我想寫一個簡單的STL迭代器CArray MFC類使用boost迭代器適配器。這是我的代碼:編譯器錯誤與boost迭代器適配器

#include <boost/iterator/iterator_adaptor.hpp> 
#include <afxtempl.h> 

class CArrIter : public boost::iterator_adaptor< CArrIter , 
    int, 
    int, 
    boost::random_access_traversal_tag > 
{ 
public: 
    CArrIter(CArray<int,int>& arr, int index = 0) : m_arr(arr) 
    { 
     this->base_reference() = index; 
    } 

private: 
    friend class boost::iterator_core_access; 
    int dereference() const{ 
     return m_arr.GetAt(base()); 
    } 


private: 
    CArray<int,int>& m_arr; 
}; 

這個編譯良好的VC9編譯器。但是當我嘗試用VC7編譯時,出現以下錯誤:

\include\boost\iterator\iterator_traits.hpp(49) : erro r C2039: 'difference_type' : is not a member of 'boost::detail::iterator_traits< Iterator>' with [ Iterator=int ]

\include\boost\mpl\eval_if.hpp(41) : see refer ence to class template instantiation 'boost::iterator_difference' bein g compiled with [ Iterator=int ]

.... Some more ....

任何線索什麼可能是錯誤的?我必須包含一些其他頭文件?我很興奮提升圖書館。

回答

0

它可能與隨機訪問行爲沒有包含遍歷容器所需的所有內容有關。這個環節的「iterator_adaptor要求」部分可能會有所幫助:

Boost: Iterator Adapter

我不知道,如果int是分配的,所以我不知道,如果你改變INT爲int &會發生什麼。

一對夫婦更多的想法:

  • 您是否正在對兩種編譯Boost庫相同版本的?
  • 是否使取消引用()受保護或公共幫助?
4

我認爲boost :: iterator_adaptor <>的第二個模板參數必須是有效的迭代器類型,請嘗試使用int *而不是int。

+0

的確,第二個參數(Base)將被用來確定其餘的默認類型,並通過等價的'std :: iterator_traits'來傳遞它。由於OP沒有指定'difference_type',因此當boost使用'boost :: detail :: iterator_traits'' – UncleBens 2010-02-01 16:18:51

+0

'嘗試執行編譯時,編譯失敗,但是爲什麼它會使用VC9編譯呢? – Naveen 2010-02-02 04:29:48

+0

@Naveen:我不知道。但現在我想到了,也許你需要的實際上是iterator_facade? http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/iterator_facade.html – Manuel 2010-02-02 08:39:38

相關問題