1

我想迭代一个QMultiMapusing

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

但编译器抱怨

error: expected ‘;’ before ‘it’

导致

error: ‘it’ was not declared in this scope

每次使用。我试过了ConstIteratorconst_iterator连慢的Iterator都没有成功。甚至可以将 Q(Multi)Map 与模板类一起使用吗?当定义(作为 void*)可以时,为什么我不能声明一个迭代器?

我使用以下代码(包括警卫省略):

#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
};
4

1 回答 1

2

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;

它会建立。

于 2011-09-18T13:34:36.993 回答