Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top