首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以在自己的模板类中使用QMultiMap::ConstIterator?

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

Stack Overflow用户
提问于 2011-09-18 21:28:55
回答 1查看 481关注 0票数 1

我想使用以下命令遍历QMultiMap

代码语言:javascript
复制
QMultiMap<double, TSortable>::const_iterator it;`

但是编译器抱怨说

代码语言:javascript
复制
error: expected ‘;’ before ‘it’

导致了一个

代码语言:javascript
复制
error: ‘it’ was not declared in this scope

对于每一次使用。我尝试了ConstIteratorconst_iterator,甚至速度较慢的Iterator,但都没有成功。有没有可能在模板类中使用Q(Multi)Map?为什么我不能声明一个迭代器,当定义(作为void*)是可以的?

我使用下面的代码(忽略了guard ):

代码语言:javascript
复制
#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
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-18 21:34:37

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

代码语言:javascript
复制
error: need ‘typename’ before ‘QMultiMap<double, TSortable>::const_iterator’ because ‘QMultiMap<double, TSortable>’ is a dependent scope

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

代码语言:javascript
复制
typename QMultiMap<double, TSortable>::const_iterator it;

它将会构建。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7461779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档