call to non-static member function without an object argument
-
I want to make file downloader function that can be called in any place in the programm
But I'm getting that error: call to non-static member function without an object argument
What's wrong
download.h:
#include <QtCore> #include <QtNetwork> #include <cstdio> #ifndef DOWNLOAD_H #define DOWNLOAD_H QT_BEGIN_NAMESPACE class QSslError; QT_END_NAMESPACE using namespace std; class download: public QObject { Q_OBJECT QNetworkAccessManager manager; QVector<QNetworkReply *> currentDownloads; public: QString dir; download(); void doDownload(QUrl url, QString directoryName); static QString saveFileName(const QUrl &url); bool saveToDisk(const QString &filename, QIODevice *data); static bool isHttpRedirect(QNetworkReply *reply); public slots: void execute(); void downloadFinished(QNetworkReply *reply); void sslErrors(const QList<QSslError> &errors); }; #endif // DOWNLOAD_H
download.cpp:
#include "download.h" download manager(); download::download() { connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*))); QTimer::singleShot(0, &manager, SLOT(execute())); } void download::doDownload(QUrl url, QString directoryName) { dir = directoryName; QNetworkRequest request(url); QNetworkReply *reply = manager.get(request); #if QT_CONFIG(ssl) connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>))); #endif currentDownloads.append(reply); } QString download::saveFileName(const QUrl &url) { QString path = url.path(); QString basename = QFileInfo(path).fileName(); if (basename.isEmpty()) basename = "download"; if (QFile::exists(basename)) { // already exists, don't overwrite int i = 0; basename += '.'; while (QFile::exists(basename + QString::number(i))) ++i; basename += QString::number(i); } return basename; } bool download::saveToDisk(const QString &filename, QIODevice *data) { QString filePath = dir + '/' + filename; QFile file(filePath); if (!file.open(QIODevice::WriteOnly)) { fprintf(stderr, "Could not open %s for writing: %s\n", qPrintable(filename), qPrintable(file.errorString())); return false; } file.write(data->readAll()); file.close(); return true; } bool download::isHttpRedirect(QNetworkReply *reply) { int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); return statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 305 || statusCode == 307 || statusCode == 308; } void download::execute() { QUrl url = QUrl::fromEncoded(QString("http://downloads.softwarerat.de/Expsoska.rar").toLocal8Bit()); } void download::sslErrors(const QList<QSslError> &sslErrors) { #if QT_CONFIG(ssl) for (const QSslError &error : sslErrors) fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString())); #else Q_UNUSED(sslErrors); #endif } void download::downloadFinished(QNetworkReply *reply) { QUrl url = reply->url(); if (reply->error()) { fprintf(stderr, "Download of %s failed: %s\n", url.toEncoded().constData(), qPrintable(reply->errorString())); } else { if (isHttpRedirect(reply)) { fputs("Request was redirected.\n", stderr); } else { QString filename = saveFileName(url); if (saveToDisk(filename, reply)) { printf("Download of %s succeeded (saved to %s)\n", url.toEncoded().constData(), qPrintable(filename)); } } } currentDownloads.removeAll(reply); reply->deleteLater(); if (currentDownloads.isEmpty()) { // all downloads finished QCoreApplication::instance()->quit(); } } #include "main.moc"
main.cpp:
#include <QtCore> #include <QtNetwork> #include <cstdio> #include <download.h> int main(int argc, char **argv) { QUrl bruh = QUrl::fromEncoded(QString("http://downloads.softwarerat.de/Expsoska.rar").toLocal8Bit()); download::doDownload(bruh, QString("http://test.com/file.zip")); } #include "main.moc"
problem with that line of code in main.cpp: download::doDownload(bruh, QString("TEST"));
Help please.
-
@InferusAnima
Either you need to create an instance ofdownload
, or you need to rewrite astatic
version. -
@InferusAnima
Even this error is fixed, this application still cannot work.
It has no event loop, the asynchronous network operations won't survive.
Since you are using Qt classes, when starting a new project, you should choose Qt Widgets/Console Application templates.
They'll give you a QCoreApplication/QApplication to start the event loop. -
@InferusAnima
Well, that should be easy.
First, add an extern declaration on the bottom of download.h:extern download manager;
Second on the top of download.cpp
//change to this if get error on this line: download manager() download manager;
Last in the main.cpp
manager.doDownload(bruh, QString("http://test.com/file.zip"));
You may also need to delete this bottom line in the .cpps
#include "main.moc"
-
@vatsk Please read and follow https://forum.qt.io/topic/113070/qt-code-of-conduct else you will be baned!
I will delete your post as inappropriate and offensive.