我想使用以下命令遍历QMultiMap
QMultiMap<double, TSortable>::const_iterator it;`但是编译器抱怨说
error: expected ‘;’ before ‘it’导致了一个
error: ‘it’ was not declared in this scope对于每一次使用。我尝试了ConstIterator,const_iterator,甚至速度较慢的Iterator,但都没有成功。有没有可能在模板类中使用Q(Multi)Map?为什么我不能声明一个迭代器,当定义(作为void*)是可以的?
我使用下面的代码(忽略了guard ):
#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
};发布于 2011-09-18 21:34:37
在你引用的那个错误之前,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;它将会构建。
https://stackoverflow.com/questions/7461779
复制相似问题