[Qt] error: call to non-static member function without an object argument


error: calls a non-static member function with no object arguments.
this error occurs because the class is not instantiated with .
Take a chestnut, for example:

 class Student
 {
 public:
 int getAge();
 };

The reason for this error may be that when you call the function getAge(), you are doing this:

 Student::getAge();

Or do it this way:

  Student.getAge();

This means that the member function is called without instantiating the class Student.
Fix one: Instantiate an object first

 Student XiaoWang;
 int age = XiaoWang.getAge();

Correction 2: Declare member functions as static functions

 class Student
 {
 public:
static  int getAge(); 
 };
 
 Student::getAge();

Take chestnuts, for example:


  QDir::mkpath(Path);

Error: Calling a non-static member function with no object arguments.
the reasons for this error is that the class does not instantiate and call mkpath function need to first class instantiation,
to:

   QDir tempDir;   
    tempDir.mkpath(Path); 

That’s right.
If you write your own class, you can also add the static keyword before the function’s declaration in the class body to declare the function as a static member and call it without instantiation.
such as:


QMessageBox::warning(this,tr("Warning"),tr("No Exist"));

Static member functions can be called directly with “ class :: function name “.

Read More: