【问题标题】:Is it possible to use to use QMultiMap::ConstIterator in own template class?是否可以在自己的模板类中使用 QMultiMap::ConstIterator?
【发布时间】:2011-09-18 13:28:55
【问题描述】:

我想使用

遍历 QMultiMap
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
};

【问题讨论】:

    标签: c++ qt iterator qmap qmultimap


    【解决方案1】:

    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;
    

    它会构建。

    【讨论】:

    • 你是对的,不幸的是(事实证明......)我的 GCC(4.4.4 ubuntu)即使使用 -Wall 也没有显示该错误
    猜你喜欢
    • 2015-10-06
    • 2016-02-08
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多