我想使用
迭代QMultiMap
QMultiMap<double, TSortable>::const_iterator it;`
但编译器抱怨
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
};
答案 0 :(得分: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;
它将构建。