Results 1 to 6 of 6

Thread: Cannot call function without object

  1. #1
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Cannot call function without object

    Hi. I've got a problem. I have public member function called clear() which looks like this:
    Qt Code:
    1. void MainWindow::clear()
    2. {
    3. tableWidget->clear();
    4. tableWidget_2->clear();
    5. tableWidget_3->clear();
    6. tableWidget_4->clear();
    7. tableWidget_5->clear();
    8. tableWidget_6->clear();
    9. tableWidget_7->clear();
    10. }
    To copy to clipboard, switch view to plain text mode 

    and when I call it in another member function
    Qt Code:
    1. MainWindow::clear()
    To copy to clipboard, switch view to plain text mode 

    There appear error that I can't call member function MainWindow::clear() without object. What's the problem about? Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot call function without object

    You need something like this:
    Qt Code:
    1. this->clear();
    To copy to clipboard, switch view to plain text mode 
    or simply call clear().

    The class resolution operator ( :: ) is used when calling static functions.

    But usually, when you're using other member functions, you can just call them. Using "this" is a well known ( some say bad ) Java practice.

    Regards

  3. #3
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot call function without object

    I tried to use only call() but there appear error that clear() is not declared

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot call function without object

    Quote Originally Posted by Salazaar View Post
    I tried to use only call() but there appear error that clear() is not declared
    Where do you want to invoke MainWindow::clear() (i.e. in what method or function)?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot call function without object

    Quote Originally Posted by marcel View Post
    Using "this" is a well known ( some say bad ) Java practice.
    Not only Java, but Python and PHP too. It's completely unnecessary and makes your code look ugly, but I wonder whether I could have any positive outcomes.

  6. #6
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot call function without object

    If the clear function is within QMainWindow why not put the following in your QMainWindow header file :

    Qt Code:
    1. extern MainWindow *theApp;
    To copy to clipboard, switch view to plain text mode 

    and then in your .cpp file for MainWindow, someplace at the top :

    Qt Code:
    1. MainWindow *theApp; // I'm presuming MainWindow derives from QMainWindow...
    To copy to clipboard, switch view to plain text mode 

    and in your constructor :

    Qt Code:
    1. theApp = this;
    To copy to clipboard, switch view to plain text mode 

    This way, you can make calls to the clear function from other classes by :

    Qt Code:
    1. theApp->clear();
    To copy to clipboard, switch view to plain text mode 

    as long as you include the MainWindow header file...

    Regards,
    Steve

Similar Threads

  1. Replies: 3
    Last Post: 16th May 2007, 12:07
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 09:52
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26

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.