简体   繁体   中英

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

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();
                                                                                ^

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM