c++qtbase64qt5

Base64 encoding of String in Qt 5


I am trying to base64 encode a QString in Qt5 . However, I am getting an error saying identifier not found on line QString b64string = base64_encode(src);

#include <QCoreApplication>
#include <QByteArray>
#include <QBitArray>
#include <QString>
#include <QDebug>

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


    QString src = "Hello";
    QString b64string = base64_encode(src);

    qDebug() << "Encoded string is" << b64string;
    return a.exec();
}

QString base64_encode(QString string){
    QByteArray ba;
    ba.append(string);
    return ba.toBase64();
}

Why is the error occurring? can someone point out my mistake?


Solution

  • The identifier it can't find is base64_encode. This is because it doesn't come until later in the file. The usual way of preventing this error is to put a function prototype at the beginning of the file or in a separate include header:

    QString base64_encode(QString string);
    

    You could also just rearrange the code so that anything depending on the definition comes last, i.e. move main to the end.