Qt Error: ‘string’ was not declared in this scope.

Oddly enough, I ran into this same problem while using SFML. Here in Qt-creator, I’m getting this error while trying to use a string. I declared something like so:

#include <QCoreApplication>
#include <QDebug>
#include <QList>

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

    QList<String> myStringList;

    myStringList << "Howdy."
                 << " I "
                 << " like this"
                 << " course on Qt.";

    myStringList.append(" I hope");
    myStringList.append(" you do too!");

    //qDebug() << myStringList[1];

    for (int i=0; i < myStringList.count(); i++) {
        qDebug() << myStringList[i];
    }

    return a.exec();
}

And I get the error that the string is not declared in this scope! So, I tried adding:

#include <string>

to add the string library from C++. However, then I got errors when returning those strings to qDebug, which said it couldn’t take a ‘char’ as a ‘string’.

Well, just like SFML, Qt has its very own string library, and it is automatically included, called QString, like this:

QList<QString> myStringList;

QString automagically changes the string to character or strings or whatever to make it work when you need it to. Seems like a bit of laziness for me as a programmer, but pretty handy and very convenient in my Qt apps!

Linux – keep it simple.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s