是否可以在自己的模板类中使用QMultiMap::ConstIterator ?

Is it possible to use to use QMultiMap::ConstIterator in own template class?

本文关键字:QMultiMap ConstIterator 自己的 是否      更新时间:2023-10-16

我想使用

迭代QMultiMap
QMultiMap<double, TSortable>::const_iterator it;`

但是编译器报错

error: expected ‘;’ before ‘it’

导致

error: ‘it’ was not declared in this scope

用于每次使用。我尝试了ConstIterator, const_iterator,甚至较慢的Iterator,没有任何成功。它甚至可以使用Q(多)地图与模板类?为什么我不能声明一个迭代器当定义(作为void*)是ok的?

我使用以下代码(省略了包含保护):

#include <QtCore/QDebug>
#include <QtCore/QMap>
#include <QtCore/QMultiMap>
#include <limits>
/** TSortable has to implement minDistance() and maxDistance() */
template<class TSortable>
class PriorityQueue {
public:
  PriorityQueue(int limitTopCount)
      : limitTopCount_(limitTopCount), actMaxLimit_(std::numeric_limits<double>::max())
  {
  }
  virtual ~PriorityQueue(){}
private:
  void updateActMaxLimit(){
    if(maxMap_.count() < limitTopCount_){
      // if there are not enogh members, there is no upper limit for insert
      actMaxLimit_ = std::numeric_limits<double>::max();
      return;
    }
    // determine new max limit
    QMultiMap<double, TSortable>::const_iterator it;
    it = maxMap_.constBegin();
    int act = 0;
    while(act!=limitTopCount_){
      ++it;// forward to kMax
    }
    actMaxLimit_ = it.key();
  }
  const int limitTopCount_;
  double actMaxLimit_;
  QMultiMap<double, TSortable> maxMap_;// key=maxDistance
};

GCC在你引用的错误之前给出这个错误:

error: need ‘typename’ before ‘QMultiMap<double, TSortable>::const_iterator’ because ‘QMultiMap<double, TSortable>’ is a dependent scope

解释了这个问题。添加typename关键字:

typename QMultiMap<double, TSortable>::const_iterator it;

,它会生成