Let’s begin our first example to elaborate on how we get the error “cannot call member function without object” in C++ code. So, the first thing we need to do is to create a c++ file. This file can be created with some text editor within the file explorer or within the shell. You can try opening it with the vim editor, text editor, or some other editor like GNU Nano editor. So, we have been directly opening this newly created file in GNU Nano editor using the “nano” command. Now, the empty newly created file is launched in the Nano editor and ready to be used.
We have been starting our code with the addition of some header files, i.e., “iostream”. You can use the “std” namespace in the code while declaring it before the main function. Otherwise, you have to use the keyword “std” with every cout and cin clause in the program. So, we have initialized it before the main method using the “using” keyword. We have created a simple class named “A” in the code. This class contains a single user-defined function named “show()”. The function contains the declaration of a character type variable named “a”. The first cout statement of this function asks for a character to be added by a user. The “cin” clause allows a user to add that character on the shell and save it to the variable “a”. The last cout statement has been used here to display the character on the shell that the user has input.
Now the class has ended, and we have started the main method. It’s time to call the function to execute it without using anything else. So, we have simply used the class name “A” with “::” to call the function “show()” in the main method. The main method is closed here. We are ready to save this code with Ctrl+S as it is already complete.
Get back to the terminal by using “Ctrl+X. Now, it’s high time to compile the code with the c++ built-in compiler of Ubuntu 20.04. Thus, we have used the “g++” compiler instruction here to simply compile the code file and see whether it is error-free or not. On compilation, it shows the error. This means we cannot just execute or call the function show() of class “A” without creating an object of this class.
So, we need to update the code file once again. Make use of the nano editor once again and resolve this error. So the old file is opened again. We need to only change the main() function to elude this error so far. So, we have created an object “obj” of class “A”. Now, this object “obj” has been used here to call the function show() of the class “A” using the “dot” between the object name and function name. Save the code to reflect the changes on execution using the simple shortcut key, i.e., “Ctrl+S”. The updated code has been displayed in the image below. Let’s come back to the terminal to compile the updated code using “Ctrl+X”.
Used the “g++” compiler instruction for the purpose of code compilation. We have got no error this time. On execution of the code, we have got the result shown below. It asked to enter the character, adding “A”. In return, it displayed the character on the shell. Using the object to call the function show().
Let’s have another example of getting the same error and solving it afterward. So, we opened the file error.cc and created the below-shown code. This code has been started from the iostream header file, std namespace, and ended on the main method. Our program contains two classes, A and B. A is parent class, and B is child class of A inheriting its properties here. Parent class A contains a function “print()” having a single statement displaying that this is a parent class function. The child class contains a function “show()” with a cout statement displaying that the child class method has been executed. The main() function contains a simple “::” method to call both the methods using their respective classes, i.e., “A” and “B”. Let’s just run this code to see how it works.
When we compiled the code, we got the exception error saying “cannot call member function without object” for both the function calls. This is because we did not create the objects to call the functions of respective classes and did it directly.
Let’s just open the file once again using the “nano editor”. There is no need to change the whole code; only the main() method requires a little modification. As you can have a look that, we have created an object obj1 of parent class “A” and used it to do a function call to print() method. Then, we created an object “obj2” of child class B and did a function call to function “show()” here to execute it. We can also avoid making the parent class object “obj1” and still call its function by using the child class object “obj2”. Both the functions in parent and child classes have different names, so it will not cause any error. Let’s just save the code file with Ctrl+S and come back to the terminal to see if the error is resolved or not.
After the compilation of updated code, we can have a glance that the error “cannot call member function without its object” has been removed finally, and the code is ready to be executed. Both parent and child class functions were executed as shown upon running the code.
This article contains a simple demonstration of creating an error “cannot call member function without the object” in the c++ program. We have used the concept of classes here to make it possible. Then, we have also demonstrated the solution to resolve this error within the examples. We are quite hopeful that this article will be helpful to all the c++ naïve users.