1

I have some code that is meant to get a QList of QMaps from a set of data. Then it should go through all the QMaps in that list. For some reason upon attempting this I am getting some errors regarding the QVector used to store data. Here is my code:

#include <QCoreApplication>
#include <QMap>
#include <QVariant>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<QMap<QString, QVariant>> maps;

    QMap<QString, QVariant> item1;
    item1.insert("Item 1", QVariant::fromValue(1));

    maps.append(item1);

    foreach (const QMap<QString, QVariant> & map, maps)
    {
        qDebug() << map;
    }

    return a.exec();
}

Error:

..\ErrorTest\main.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
..\ErrorTest\main.cpp(17): error C2275: 'QVariant': illegal use of this type as an expression
C:\Qt\5.8\msvc2015_64\include\QtCore/qsharedpointer_impl.h(94): note: see declaration of 'QVariant'
..\ErrorTest\main.cpp(17): error C2955: 'std::remove_reference': use of class template requires template argument list
D:\Microsoft Visual Studio 14.0\VC\INCLUDE\xtr1common(301): note: see declaration of 'std::remove_reference'
..\ErrorTest\main.cpp(17): error C2065: 'map': undeclared identifier
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ')' before '>'
..\ErrorTest\main.cpp(17): error C2059: syntax error: '>'
..\ErrorTest\main.cpp(17): error C2065: '_container_': undeclared identifier
..\ErrorTest\main.cpp(17): error C2228: left of '.control' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2228: left of '.i' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2228: left of '.e' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2059: syntax error: ')'
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ';' before 'for'
..\ErrorTest\main.cpp(17): error C2059: syntax error: '='
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ';' before '}'
..\ErrorTest\main.cpp(17): fatal error C1004: unexpected end-of-file found

Thank you, and yes the tagData->toMappedList() returns the correct set of QMaps/data.

Nicholas Johnson
  • 1,012
  • 2
  • 12
  • 35
  • 1
    use: `for (auto map: maps) { qDebug() << map; }` – eyllanesc May 11 '17 at 19:48
  • @eyllanesc Did you try the `auto` version? This was the second thing that came to my mind as a possible solution however I get an error (see my answer). – rbaleksandar May 11 '17 at 19:58
  • 1
    @rbaleksandar `foreach` is a macro that is no longer recommended to use, it is better `for()`, and me if it works for me. – eyllanesc May 11 '17 at 20:00
  • 1
    "no longer recommended to use" - for reasons such as this very question among others. :D But it still baffles me why I can't get it to work with this code. PS: I know it's a macro. :P – rbaleksandar May 11 '17 at 20:03

1 Answers1

1

Imho the problem is the , inside the template. Sadly foreach is nothing but a call for Q_FOREACH which is a macro. It requires to arguments separated by comma. But because of the template you have 2 commas. I had this problem a while ago, though Qt Creator at least provided me with error: macro "Q_FOREACH" passed 3 arguments, but takes just 2 instead of the ton of errors you got. In your case this would be:

..\ErrorTest\main.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'

I would suggest a for loop with an iterator to traverse the list of maps. Unless you want to dump the const in which case you can do:

QMap<QString, QVariant> map;
foreach (map, maps) {
    ...
}

You can also use auto but again - no constantness.

If constantness is a critical aspect in your case, go for the for loop with a constant iterator.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Thank you very much, I used the `QMap map;` as you said and it works very well! – Nicholas Johnson May 11 '17 at 19:52
  • @NicholasJohnson I know it works. I just tested it. :D But again note the loss of constantness. In your question you have attempted to use `const`. This prevents editing the given map inside the loop. I'm guessing you did that on purpose. If so you should consider using the constant iterator and for-loop. – rbaleksandar May 11 '17 at 19:54
  • When I do the `auto map, maps` it works fine, I just like the first solution that you provided. – Nicholas Johnson May 11 '17 at 19:59
  • Hmmm, I guess my setup is screwed. I though that it was the missing `c++11` configuration in my `qmake` but it didn't fix a thing after adding it. Will have to investigate further. Thanks for the feedback. – rbaleksandar May 11 '17 at 20:00
  • No problem! I also have the += `c++11` flag in my project file – Nicholas Johnson May 11 '17 at 20:00