PDA

View Full Version : Cannot call function without object



Salazaar
10th June 2007, 20:23
Hi. I've got a problem. I have public member function called clear() which looks like this:

void MainWindow::clear()
{
tableWidget->clear();
tableWidget_2->clear();
tableWidget_3->clear();
tableWidget_4->clear();
tableWidget_5->clear();
tableWidget_6->clear();
tableWidget_7->clear();
}

and when I call it in another member function

MainWindow::clear()

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

marcel
10th June 2007, 20:26
You need something like this:


this->clear();
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

Salazaar
10th June 2007, 20:30
I tried to use only call() but there appear error that clear() is not declared

jacek
10th June 2007, 20:58
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)?

jacek
10th June 2007, 21:03
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.

steg90
11th June 2007, 15:55
If the clear function is within QMainWindow why not put the following in your QMainWindow header file :



extern MainWindow *theApp;


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



MainWindow *theApp; // I'm presuming MainWindow derives from QMainWindow...


and in your constructor :



theApp = this;


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



theApp->clear();


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

Regards,
Steve