Question

I have QHash<QString, QHash<quint64, QElapsedTimer*> myNestedQHash; and when I try

foreach (QHash<quint64, QElapsedTimer*>  stat, myNestedQHash.values(someStr))

I get

error: macro "Q_FOREACH" passed 3 arguments, but takes just 2

Isn't it possible to loop on nested QHash they way I did?

Was it helpful?

Solution

why not using

for (QHash<QString, QHash<quint64, QElapsedTimer*>::iterator it = myNestedQHash.begin(); it != myNestedQHash.end(); ++it) 
{...}

instead? i think Q_FOREACH will create a copy, so it will be better performance as well...

/edit:

the foreach is just a definition for the Q_FOREACH macro ... so the compiler sees it and it will accept 2 values. since you have a additional comma in it, it will see 3 arguments. you will find all infos here.

OTHER TIPS

Should be working like this:

QHash<QString, int> myHash0;
myHash0["test0"]=0;
myHash0["test1"]=1;
QHash<QString, int> myHash1;
myHash1["test0"]=0;
myHash1["test1"]=1;

QHash<QString, QHash<QString, int> > myHashList;
myHashList["Hash0"] = myHash0;
myHashList["Hash1"] = myHash1;

QHash<QString, int> h;
foreach(h , myHashList)
{
    qDebug()<<h["test0"];
}

QT foreach is a macro. And parameters in a macro are separated by comma ,

In your case, you use a template with comma inside.

You can write it as:

QHash<quint64, QElapsedTimer*>  stat;  
foreach (stat, myNestedQHash.values(someStr))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top