2011-09-18 48 views
1

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

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

遍历一个QMultiMap但是编译器会抱怨

error: expected ‘;’ before ‘it’ 

导致

error: ‘it’ was not declared in this scope 

在每次使用。我试过ConstIterator,const_iterator甚至更​​慢的Iterator没有任何成功。是否可以使用Q(多)地图与模板类?为什么我无法在定义(如void *)定义时声明Iterator?

我用下面的代码(包括后卫略):

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

回答

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; 

它会建立。

+0

你是对的,不幸的是(事实证明...)我的GCC(4.4.4 ubuntu)即使使用'-Wall'也不显示该错误 – mbx

相关问题