C++

C++ ERROR a Nonstatic Member Reference Must Be Relative to a Specific Object

Today, we will discuss an error that may occur due to a lack of practice by a programmer when they mix the concept of static and non-static functions. Let us first study non-static member functions. These are the type of functions that are declared in the member specification area of any class without any static specifier. For the non-static members, there is a special method for the declaration. In C++, if we need to call any function from any class, we can call it by creating an object. If it is the static member, then it will be called by the double colons “::” between the class and the function name like class_name::func_name. If it is the non-static function, then using this will give an error that is “ERROR a non-static member reference must be relative to a specific object”.

Sometimes it becomes hectic to tackle this kind of error when the new programmers are using a non-static function. But once they get familiar with this, it becomes easy to resolve these errors. To resolve this error there are two methods. The first one is to change the function to the static function and call it by simply using the calling method “class_name::function_name”. Or the second method, that is we have to create an object of that class and then using the object name we can call the function by the function called “class_object.function_name”.

Syntax:

There is no specified syntax for this error because it is not a part of the code it is the compile time error that may occur due to bugs in the code.

error: cannot call member function ‘Function’ without object

The above shown is an image of what the error looks like.

Example # 01:

Now, we will be performing an example to check how this error is caused and how to resolve this type of error. Before moving to our main code, where the error occurs, we will first include the header file “iostream” that is used to enable us to perform I/O operations. After that, we created a class object named “my_object”. Inside this class, we declared a static member named “display()” and also a non-static member function “show()”.

Outside the class, we have called both functions using the class “my_object”. Inside the display() member function, we have printed the message of execution for the display() method. And in the show() method, we have instructed the compiler to print the execution message for the show() method. Heading towards our main function, where we will call both functions using the static member calling method that is the name of the class then the double colons along with the method name. In our case, it looks like “my_object::display();”. We called our show() function and returned the null value, we will now execute the code.

#include <iostream>
using namespace std;
class my_object {
public:
    static void display();
    void Show();
};
void my_object::display() {
    cout << "display() method executed" << endl;
}
void my_object::Show() {
    cout << "Show() method executed" << endl;
}
int main() {
    my_object::display();
    my_object::Show();
    return 0;
}

After executing our code, we encountered this error displayed on line “21” where we called our “show()” method. The error that was encountered is due to the function call because it is a non-static member and we called it using the static member call method. Now we will resolve this error using the accurate way to call the non-static function.

As shown in the snippet below, we have replaced the function call with the correct one by first creating an object to that class. Then, using this object, we called the function by reference to that object “obj”. Now, we execute the code again.

After executing the code again, we successfully displayed both function’s messages as we can see in the figure below this time no error has been displayed.

Example # 02:

Now, we will implement another example in which we will create a non-static function in which we will perform a calculation. After including our header file, we will create a class named “myClass” in which we declared a static member named “My_func()” and a non-static member “add()”. Outside that class, we called both functions. In the add() function, we will perform the addition of two integer variables “a” and “b” to which we assigned the values “2” and “3” and store them in an integer variable “c”. At the end, using the “cout” statement, we will print the sum of both values. In the second function My_func(), we will simply display the message from which function it is displayed. Now, diving into our main function where using the static method call, we will call both functions. After calling those methods, we will execute the code.

#include <iostream>
using namespace std;
class myClass
{
public:
   static int My_func();
  void add();
};
void myClass::add()
{
    int a=2, b=3;
    int c= a+b;
    std::cout <<"the sum of a and b is: "<< c << std::endl;
}
int myClass::My_func()
{
   std::cout << "this is my_func() method" << std::endl;
   return 0;
}
int main(){
    myClass::My_func();
    myClass::add();
}

Below is the error message that is displayed in our output portion which indicates that there is a wrong function call on line 22 for the myClass::add() method which has been called without creating an object for that class. As we discussed above, we cannot call a non-static member without creating an object. We will now try to resolve this error.

In the given figure, we have written the correct way to call a non-static member. We first created an object for the class of which the function will be called. We declared the object “obj” for “myClass” with the reference of which we will call that function. After creating the object, we called the function using the function called “obj.add()” and then execute the code.

After the execution of the code, we successfully calculated the addition of the variables that we have performed in the “add()” method and displayed the result using the variable “c” that is holding the return value of the addition.

Conclusion

In this guide, we have studied the compile time error that mostly occurs due to the function call of the non-static member function. This error mostly occurs due to the misconception of the coder about the non-static members. We explained the way to resolve these errors by implementing multiple examples.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.