Results 1 to 8 of 8

Thread: invalid use of this in non member function

  1. #1
    Join Date
    Jan 2011
    Location
    Nagercoil,Tamilnadu,India
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default invalid use of this in non member function

    hi i want to do a qt mobile apps which using network access but while compiling my code it shows the following errors.

    i. invalid use of this in non member function
    ii. connect was not declared in this scope

    I have do the following in my code:

    .pro file:

    Qt Code:
    1. QT += network
    To copy to clipboard, switch view to plain text mode 

    main.cpp file

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QtNetwork>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9.  
    10. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    11. connect(manager, SIGNAL(finished(QNetworkReply*)),
    12. this, SLOT(replyFinished(QNetworkReply*)));
    13.  
    14. manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
    15.  
    16. #if defined(Q_WS_S60)
    17. w.showMaximized();
    18. #else
    19. w.show();
    20. #endif
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 


    Is it any problem with my code please help me. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: invalid use of this in non member function

    connect() is a method of QObject and not a standalone function.
    Qt Code:
    1. QObject::connect(manager, SIGNAL(finished(QNetworkReply*)),
    2. this, SLOT(replyFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 19th January 2011 at 10:36.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    dineshkumar (19th January 2011)

  4. #3
    Join Date
    Jan 2011
    Location
    Nagercoil,Tamilnadu,India
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: invalid use of this in non member function

    hi thanks,
    But it again shows the following error:

    invalid use of this in non member function

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: invalid use of this in non member function

    There is no "this" in main().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    dineshkumar (19th January 2011)

  7. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: invalid use of this in non member function

    Qt Code:
    1. replyFinished(QNetworkReply*)
    To copy to clipboard, switch view to plain text mode 
    where is this function defined? you need that class object to be passed in connect instead of "this", the same as you pass "manager".

  8. The following user says thank you to nish for this useful post:

    dineshkumar (19th January 2011)

  9. #6
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: invalid use of this in non member function

    It is probably easier if you move your code to the MainWindow instead:

    main.cpp:
    Qt Code:
    1. // main.cpp
    2.  
    3. #include <QtGui/QApplication>
    4. #include "mainwindow.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. MainWindow w;
    10.  
    11. #if defined(Q_WS_S60)
    12. w.showMaximized();
    13. #else
    14. w.show();
    15. #endif
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h:
    Qt Code:
    1. // mainwindow.h
    2.  
    3. #ifndef MAINWINDOW_H
    4. #define MAINWINDOW_H
    5.  
    6. #include <QtGui/QMainWindow>
    7.  
    8. class QNetworkReply;
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. protected slots:
    19. void replyFinished(QNetworkReply *reply);
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. // mainwindow.cpp
    2.  
    3. #include <QtNetwork/QtNetwork>
    4. #include <QtNetwork/QNetworkReply>
    5. #include "mainwindow.h"
    6. #include <QDebug>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    9. {
    10. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    11. connect(manager, SIGNAL(finished(QNetworkReply*)),
    12. this, SLOT(replyFinished(QNetworkReply*)));
    13.  
    14. manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. }
    20.  
    21. void MainWindow::replyFinished(QNetworkReply *reply)
    22. {
    23. qDebug() << "ok!";
    24. }
    To copy to clipboard, switch view to plain text mode 

  10. The following 2 users say thank you to helloworld for this useful post:

    dineshkumar (20th January 2011), tylor2000 (13th December 2015)

  11. #7
    Join Date
    Jan 2011
    Location
    Nagercoil,Tamilnadu,India
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: invalid use of this in non member function

    Thankyou friend its works..

  12. #8
    Join Date
    Dec 2015
    Posts
    1
    Thanks
    1

    Post Re: invalid use of this in non member function

    Thank you helloworld, this helped me out a lot. You will probably never see this but just wanted to thank you for the code example. It's still helping people out after all these years.

Similar Threads

  1. Replies: 0
    Last Post: 24th October 2010, 19:09
  2. QtConcurrent::run() with function member?
    By wookoon in forum Newbie
    Replies: 3
    Last Post: 8th July 2010, 13:12
  3. static member function
    By freekill in forum Newbie
    Replies: 4
    Last Post: 29th July 2009, 15:23
  4. Replies: 22
    Last Post: 8th October 2008, 13:54
  5. error:invalid use of member
    By quickNitin in forum General Programming
    Replies: 4
    Last Post: 19th June 2006, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.