简体   繁体   中英

QHash iterator example - no match for operator <<?

Learning QT, i`m trying to compile first example from http://qt-project.org/doc/qt-4.8/qhash-iterator.html

My code

#include <QHash>
#include <iostream>
#include <QString>

int main(int argc, char *argv[])
{
    QHash<QString, int> hash;
    hash.insert("January", 1);
    hash.insert("February", 2);
    hash.insert("December", 12);

    QHash<QString, int>::iterator i;
    for (i = hash.begin(); i != hash.end(); ++i)
        std::cout << i.key() << ": " << i.value() << std::endl;

}

is not compiling. I get this error:

main.cpp:14: error: no match for 'operator<<' in 'std::cout << i.QHash::iterator::key with Key = QString, T = int'

and i have no idea what is wrong. Code is pretty much copy-pasted from docs. Am i missing something here?

That's because std::cout doesn't work with QString . Try using QString::toStdString() , or you can also use qDebug() instead of cout

See this Q&A for details.

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