0

How can I use a QMap<QString, QString>::const_iterator as a pointer?

QMap<QString, QString>::const_iterator *i = map -> constBegin();
        while (i  !=  map -> constEnd()) {
            qDebug() << i -> key() << ": " << i -> value();

            i++;
        }
}

I get the error:

/my_class.cpp:36: error: cannot convert ‘QMap<QString, QString>::const_iterator’ to ‘QMap<QString, QString>::const_iterator*’ in initialization
         QMap<QString, QString>::const_iterator *i = map -> constBegin();
                                                                                ^
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

1 Answers1

2

The error is in this line:

QMap<QString, QString>::const_iterator *i = map -> constBegin();
                                       ~~

Here you are defining a pointer to a const_iterator but QMap::constBegin() returns just a const_iterator.

Simply remove the *, problem solved:

QMap<QString, QString>::const_iterator i = map -> constBegin();

The point is, an iterator already behaves similar to a pointer, so there is no need for the conventional syntax of defining a pointer.

Here is a great tutorial for iterators.

zett42
  • 25,437
  • 3
  • 35
  • 72