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.

SFML error: ‘string’ was not declared in this scope

2018-06-11-115438_1280x1024_scrot

In the memory game, a large right hand gives you a demo of the drum order for the round.

Work continues on my water drumming game. I’ve been learning a lot by creating this game, which was outside of the scope and tutorials found in my beginner C++ class. I’m pretty sure that my code looks as ugly as the artwork, but it is functional, which is a great feeling!

One error that I ran into confused me for a bit:

DrumGame.cpp:130:5: error: ‘string’ was not declared in this scope
string demoMode = “”;

But I have used string before and had no issues. Specifically, it came down to the fact that I’m using sfml. For some reason, there is a sf::String, and std::string. Notice the capitalization. According to SFML’s documentation:

Utility string class that automatically handles conversions between types and encodings.

sf::String is a utility string class defined mainly for convenience.

It is a Unicode string (implemented using UTF-32), thus it can store any character in the world (European, Chinese, Arabic, Hebrew, etc.).

It automatically handles conversions from/to ANSI and wide strings, so that you can work with standard string classes and still be compatible with functions taking a sf::String.

So, you either need to use String, so you can use SFML’s String, or you need to append std:: to the front of string so you can used the standard string.

Either way, the latest commit is up, where you can select “memory” from the menu, and it will demonstrate the pattern you need to repeat. Right now, since you can’t play back the pattern by clicking, it only shows you the pattern of one. The idea is once you complete that pattern, it adds another drum to your pattern for you to play next.

Stay tuned for part 2 where I will *hopefully* finish the memory mode.

Linux – keep it simple.

Fool’s Mate Friday: A friend helps speed things up….

January 30th, 2004 marked the Guinness world record for the largest networked chess computer. Using the internet to connect with 2070 computers, this networked conglomerate challenged Grand Master Peter Heine Nielsen, and the game ended in a draw!

All that to say that sometimes, we all need a little help. A “phone a friend”, if you will.

As you know, I have been working on the Beginning Chess Engine, an open source engine based in part on a tutorial by Logic Crazy, and have adapted for use on Android phones. Since the start, I have been trying to focus my efforts on making a fully functional and also responsive engine.

Unfortunately, what I have in place right now has a few bugs and is very slow. My Samsung Galaxy S4 can only handle a ply of 3 reliably, with an occasional ply of 4. Even then, these moves can take up to 3 minutes to perform, which is very slow for very little gain, by way of intelligence. However, I just received some great help from a fellow enthusiast, and GitHub-er HenriDellal.

He proposed using editable string buffer’s rather than immutable strings to speed up the computations, and it made a big difference, shaving about 10-20% off of the time needed to calculate the moves!

Using strings, which are immutable (unchanging) caused my old code to create an entirely new string, rather than just edit the existing one. The string buffer can be changed on the fly, speeding the process up immensely. Thanks HenriDellal!

You can see the commit here.

Linux – keep it simple.