Reputation: 4208
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?
Upvotes: 5
Views: 30776
Reputation: 3988
The problem you face is what Mark Ransom said, so 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 base64 I usually do this:
QString src = "Hello";
src.toUtf8().toBase64();
So you don't have to write a custom function.
Upvotes: 18
Reputation: 308130
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.
Upvotes: 10