Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. call to non-static member function without an object argument

call to non-static member function without an object argument

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 8.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    InferusAnima
    wrote on 21 Apr 2020, 19:10 last edited by InferusAnima
    #1

    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.

    JonBJ 1 Reply Last reply 21 Apr 2020, 19:36
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 21 Apr 2020, 19:32 last edited by
      #2

      Man, you should start with the C++ basics.

      I V 2 Replies Last reply 21 Apr 2020, 19:40
      1
      • I InferusAnima
        21 Apr 2020, 19:10

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 21 Apr 2020, 19:36 last edited by
        #3

        @InferusAnima
        Either you need to create an instance of download, or you need to rewrite a static version.

        1 Reply Last reply
        0
        • B Bonnie
          21 Apr 2020, 19:32

          Man, you should start with the C++ basics.

          I Offline
          I Offline
          InferusAnima
          wrote on 21 Apr 2020, 19:40 last edited by
          #4

          @Bonnie Yes I know, i'm bad at C++, but I rly need it, mb you have solution how to fix?

          B 1 Reply Last reply 21 Apr 2020, 20:03
          0
          • I InferusAnima
            21 Apr 2020, 19:40

            @Bonnie Yes I know, i'm bad at C++, but I rly need it, mb you have solution how to fix?

            B Offline
            B Offline
            Bonnie
            wrote on 21 Apr 2020, 20:03 last edited by
            #5

            @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.

            I 1 Reply Last reply 21 Apr 2020, 20:13
            0
            • B Bonnie
              21 Apr 2020, 20:03

              @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.

              I Offline
              I Offline
              InferusAnima
              wrote on 21 Apr 2020, 20:13 last edited by
              #6

              @Bonnie yeah, ik, that just test thing... i just need to fix that error. I just need ability to call download function everywhere in actual programm.

              B 1 Reply Last reply 21 Apr 2020, 20:37
              0
              • I InferusAnima
                21 Apr 2020, 20:13

                @Bonnie yeah, ik, that just test thing... i just need to fix that error. I just need ability to call download function everywhere in actual programm.

                B Offline
                B Offline
                Bonnie
                wrote on 21 Apr 2020, 20:37 last edited by
                #7

                @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"
                
                I 1 Reply Last reply 21 Apr 2020, 20:49
                0
                • B Bonnie
                  21 Apr 2020, 20:37

                  @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"
                  
                  I Offline
                  I Offline
                  InferusAnima
                  wrote on 21 Apr 2020, 20:49 last edited by
                  #8

                  @Bonnie It worked, thank you very much!

                  1 Reply Last reply
                  0
                  • B Bonnie
                    21 Apr 2020, 19:32

                    Man, you should start with the C++ basics.

                    V Offline
                    V Offline
                    vatsk
                    wrote on 3 Dec 2021, 12:31 last edited by
                    #9
                    This post is deleted!
                    jsulmJ 1 Reply Last reply 3 Dec 2021, 12:35
                    0
                    • V vatsk
                      3 Dec 2021, 12:31

                      This post is deleted!

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on 3 Dec 2021, 12:35 last edited by jsulm 12 Mar 2021, 12:37
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved