c++qtiteratorqmapqmultimap

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


I want to iterate over a QMultiMap using

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

but the compiler complains

error: expected ‘;’ before ‘it’

resulting in a

error: ‘it’ was not declared in this scope

for every usage. I tried ConstIterator, const_iterator and even the slower Iterator without any success. Is it even possible to use Q(Multi)Map with a template class? Why can't I declare an Iterator when definition (as void*) is ok?

I use the following code (include guard omitted):

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

Solution

  • GCC gives this error before the one you quoted:

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

    which explains the problem. Add the typename keyword:

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

    and it will build.