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?
The problem you face is what Mark Ransom said , just change the order of the functions or write a function prototype at the beginning of the file to solve your issue. but When i want base 64 i usually do this
QString src = "Hello";
src.toUtf8().toBase64();
so you do't have to write a custom function .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With