获取迭代器错误的位置

时间:2014-02-28 15:44:58

标签: c++ iterator containers distance

我有一些像这样的代码

forAllIter(PtrDictionary<phaseModel>, fluid.phases(), iter)
{

PtrDictionary<phaseModel>& PtrDict = fluid.phases();
int IteratorPosition = std::distance(PtrDict.begin(), iter);

}

其中'forAllIter'是具有一般结构的宏

#define forAllIter(Container,container,iter)                                   \
  441     for                                                                        \
  442     (                                                                          \
  443         Container::iterator iter = (container).begin();                        \
  444         iter != (container).end();                                             \
  445         ++iter                                                                 \
  446     )

不幸的是,在编译我的代码时,我收到了错误

error: no matching function for call to ‘distance(Foam::DLListBase::const_iterator, Foam::LPtrList<Foam::DLListBase, Foam::phaseModel>::iterator&)’

我猜问题是迭代器的类型不同?有人有解决方案或想法来解决这个问题吗?

编辑:Class PtrDictionary的代码:

namespace Foam
   48 {
   49 
   50 /*---------------------------------------------------------------------------*\
   51                         Class PtrDictionary Declaration
   52 \*---------------------------------------------------------------------------*/
   53 
   54 template<class T>
   55 class PtrDictionary
   56 :
   57     public DictionaryBase<DLPtrList<T>, T>
   58 {
   59 
   60 public:
   61 
   62     // Constructors
   63 
   64         //- Construct given initial table size
   65         PtrDictionary(const label size = 128);
   66 
   67         //- Copy construct
   68         PtrDictionary(const PtrDictionary&);
   69 
   70         //- Construct from Istream using given Istream constructor class
   71         template<class INew>
   72         PtrDictionary(Istream&, const INew&);
   73 
   74         //- Construct from Istream
   75         PtrDictionary(Istream&);
   76 };
   77 
   78 
   79 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
   80 
   81 } // End namespace Foam
   82 
   83 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
   84 
   85 #ifdef NoRepository
   86 #   include "PtrDictionary.C"
   87 #endif
   88 
   89 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
   90 
   91 #endif

EDIT2:

方法begin():

template<class T>
  363 inline typename Foam::PtrList<T>::iterator Foam::PtrList<T>::begin()
  364 {
  365     return ptrs_.begin();
  366 }

迭代器构造函数(希望这是正确的):

 iterator(LListBase_iterator baseIter)
  192             :
  193                 LList<LListBase, T*>::iterator(baseIter)
  194             {}

问候直播

1 个答案:

答案 0 :(得分:2)

没有混淆宏的重述,似乎

PtrDictionary<phaseModel>::iterator

有类型

Foam::DLListBase::const_iterator

,而

fluid.phases().begin()

返回类型为

的内容
Foam::LPtrList<Foam::DLListBase, Foam::phaseModel>::iterator&

所以某个类型正在改变...要么从fluid.phases()分配容器,要么从begin()返回迭代器