Results 1 to 12 of 12

Thread: cannot call member function without object

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default cannot call member function without object

    I get the following error when trying to connect two classes together:
    Qt Code:
    1. qapp.cpp: In static member function 'static bool QApp::eventFilter(void*)':
    2. qapp.cpp:32: error: cannot call member function 'void QApp::mouseCoord(int, int, int, int, int)' without object
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApp a(argc, argv);
    4. Widget w;
    5. w.show();
    6.  
    7. QObject::connect(&a,SIGNAL(mouseCoords(int, int, int, int, int)),
    8. &w,SLOT(mouseCoords(int, int, int, int, int)));
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    qapp.cpp:
    Qt Code:
    1. emit mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i), // this is line 32...
    2. is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
    To copy to clipboard, switch view to plain text mode 

    I have searched the forum and found several issues quite like mine, but not enough to help me out.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot call member function without object

    As the compiler error message says, you cannot call a member function, which requires a "this" pointer, from a static method that does not have a "this" pointer to give. This might do it:
    Qt Code:
    1. QApp *app = qobject_cast<QApp>(qApp);
    2. app->mouseCoords(...);
    To copy to clipboard, switch view to plain text mode 

    BTW: Is that mouseCoord() or mouseCoords() ? You aren't consistent.

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: cannot call member function without object

    If I do this:
    Qt Code:
    1. QApp *app = qobject_cast<QApp>(qApp);
    2. app->mouseCoords(...); // what is this exactly? Am I not supposed to use connect()?
    To copy to clipboard, switch view to plain text mode 
    Then what happens to this?
    Qt Code:
    1. QApp a(argc, argv);
    To copy to clipboard, switch view to plain text mode 

    Thanks for the help.

  4. #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: cannot call member function without object

    I would ask first why did you make your QApp::eventFilter() method static?
    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.


  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: cannot call member function without object

    I made it static because that's the only way it would work with QAbstractEventDispatcher(). I was experiencing some difficulties with setting QAbstractEventDispatcher() filter and a thread here in QtCentre Forum said that I should make eventFilter() static, I did and it worked. Only for that reason. But if there is a better way I would really like to know.

    EDIT:
    if I remove "static" from eventFilter I get this error message:
    Qt Code:
    1. qapp.cpp: In constructor 'QApp::QApp(int&, char**)':
    2.  
    3. qapp.cpp:14: error: no matching function for call to 'QAbstractEventDispatcher::setEventFilter(<unresolved overloaded function type>)'
    4.  
    5. c:\Qt\2010.05\qt\include\QtCore/../../src/corelib/kernel/qabstracteventdispatcher.h:91: note: candidates are: bool (* QAbstractEventDispatcher::setEventFilter(bool (*)(void*)))(void*)
    To copy to clipboard, switch view to plain text mode 
    Last edited by been_1990; 23rd October 2010 at 15:30.

  6. #6
    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: cannot call member function without object

    But this method shouldn't be static. And if you make it static then you can't call non-static methods from it. What do you need QAbstractEventDispatcher for and what is that you can't make work without making eventFilter() static?
    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.


  7. #7
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: cannot call member function without object

    I need QAbstractEventDispatcher to be able to get WM_INPUT messages because winEvenFilter won't do it.

    EDIT:
    if I remove "static" from eventFilter I get this error message:

    Qt Code:
    1. qapp.cpp: In constructor 'QApp::QApp(int&, char**)':
    2.  
    3. qapp.cpp:14: error: no matching function for call to 'QAbstractEventDispatcher::setEventFilter(<unresolved overloaded function type>)'
    4.  
    5. c:\Qt\2010.05\qt\include\QtCore/../../src/corelib/kernel/qabstracteventdispatcher.h:91: note: candidates are: bool (* QAbstractEventDispatcher::setEventFilter(bool (*)(void*)))(void*)
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: cannot call member function without object

    Can we see the exact code?
    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.


  9. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: cannot call member function without object

    Yes. Here it is:
    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QObject>
    3. #include "widget.h"
    4. #include "qapp.h"
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. //QApp *app = qobject_cast<QApp>(qApp);
    10. QApp a(argc, argv);
    11. Widget w;
    12. w.show();
    13.  
    14. QObject::connect(&a,SIGNAL(mouseCoord(int, int, int, int, int)),
    15. &w,SLOT(mouseCoord(int, int, int, int, int)));
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    qapp.cpp:
    Qt Code:
    1. extern "C"{
    2. #include "raw_mouse.h"
    3. }
    4. #include "qapp.h"
    5. #include <QDebug>
    6. #include <QAbstractEventDispatcher>
    7. #define WM_INPUT 0x00FF
    8.  
    9. QApp::QApp(int & argc, char ** argv) :
    10. QApplication(argc,argv)
    11. {
    12. qDebug() << init_raw_mouse(1, 0, 1);
    13. qDebug() << raw_mouse_count();
    14. QAbstractEventDispatcher::instance(0)->setEventFilter(eventFilter);
    15.  
    16.  
    17.  
    18. }
    19.  
    20.  
    21. bool QApp::eventFilter(void *msg)
    22. {
    23. MSG *mess;
    24.  
    25. mess = (MSG*)msg;
    26. //qDebug() << mess->message;
    27. switch(mess->message){
    28. case WM_INPUT:
    29. {
    30. add_to_raw_mouse_x_and_y((HANDLE)mess->lParam);
    31. for (int i = 0; i < raw_mouse_count(); i++){
    32. emit mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i),
    33. is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
    34. }
    35.  
    36. }
    37. return true;
    38. }
    39. return false;
    40. }
    To copy to clipboard, switch view to plain text mode 

    app.h:
    Qt Code:
    1. #ifndef QAPP_H
    2. #define QAPP_H
    3.  
    4. #include <QApplication>
    5.  
    6. class QApp : public QApplication
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit QApp(int & argc, char ** argv );
    11. //bool winEventFilter(MSG *message, long *result);
    12. static bool eventFilter(void *msg);
    13. private:
    14.  
    15. signals:
    16. void mouseCoord(int x, int y, int btn1, int btn2, int btn3);
    17.  
    18. public slots:
    19.  
    20. };
    21.  
    22. #endif // QAPP_H
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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: cannot call member function without object

    Why not do it this way?

    Qt Code:
    1. bool myInputEventFilter(void*);
    2.  
    3. class QApp : public QApplication {
    4. Q_OBJECT
    5. friend bool myInputEventFilter(void *);
    6. public:
    7. QApp(int argc, char **argv) : QApplication(argc, argv){
    8. init_raw_mouse(1, 0, 1);
    9. QAbstractEventDispatcher::instance(0)->setEventFilter(myInputEventFilter);
    10. }
    11. signals:
    12. void mouseCoord(int x, int y, int btn1, int btn2, int btn3);
    13. };
    14.  
    15. // the following function may probably be made static so that it doesn't pollute the namespace
    16. bool myInputEventFilter(void *msg) {
    17. MSG *mess = (MSG*)msg;
    18. QApp *app = (QApp*)qApp;
    19. switch(mess->message){
    20. case WM_INPUT:
    21. {
    22. add_to_raw_mouse_x_and_y((HANDLE)mess->lParam);
    23. for (int i = 0; i < raw_mouse_count(); i++){
    24. emit app->mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i),
    25. is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
    26. }
    27.  
    28. }
    29. return true;
    30. }
    31. return false;
    32. }
    To copy to clipboard, switch view to plain text mode 
    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.


  11. #11
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: cannot call member function without object

    Great! Working perfectly now. Thanks Wysota.
    What do you mean with "pollute the namespace"?

  12. #12
    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: cannot call member function without object

    Quote Originally Posted by been_1990 View Post
    What do you mean with "pollute the namespace"?
    Try defining (in a different cpp file) a function called myInputEventFilter and taking a void* as input and you'll see for yourself.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 8th October 2010, 12:38
  2. Replies: 2
    Last Post: 7th July 2009, 18:44
  3. Replies: 10
    Last Post: 29th May 2009, 10:06
  4. How to call the C++ member function from the JScript
    By parusri in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2008, 11:13
  5. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 15:55

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.