Note: The operating system used in this guide is Ubuntu 20.04. However, the same guide can be implemented on other operating systems as well.
Example 01:
Start with the first example to see how the error “function was not declared in the scope” occurs. Let’s create a new c++ file with the “touch” query in the shell, as shown in the picture below.
After creating a file, you need to open it within some editor to add C++ code to it. So, we have already installed the GNU Nano editor for this purpose. Hence, we have been utilizing Nano editor to open and write code in it as per the snap image.
Within this file, we have included an input-output stream header at the start. The standard namespace must be used in the C++ code. Because without it, our code won’t work. The main method has been initialized first to start execution. It points out that control has been given to the main method. The main method uses the function call to execute the user-defined method “Display()”. So, the Display() method that has been defined after the main method runs and outputs some text statements.
In the case of the C++ language, we have to install the g++ compiler. So, we have already installed it on our system. The g++ compiler has been utilized in the command to compile the code. The compilation throws an exception that the “Display()” method was not defined in the scope. That’s the reason our code won’t work after executing it as well.
We have to update a code to make it work and to avoid more errors of scope. The code was all correct except for one thing. To resolve this error, a first method that is helpful would be declaring the function prototype before the main() method. So, we have used the function prototype before the main method in the updated code.
When we have compiled the code, it throws no exceptions and runs properly.
$ . /a.out
Another way to resolve this scope error is to define the user-defined method before the main() function if you don’t want to use its function prototype. Hence, as per the code below, we have exchanged the positions of a main() method and Display() method.
When we have compiled the updated code, it works fine. Hence, the file code is executed successfully and shows the output on the terminal screen.
$ . /a.out
Example 02:
Let see another example to get the function scope error in the shell and resolve it. So, we have updated the code of a file “test.cc”. After the namespace and header, we have defined 2 functions, show1() and show2() in the code. The method show1() has called the method show2() within it by a function call.
Upon compiling the file, we have got the error showing that the method show2() is not declared in the scope. As the function show2() has been declared in the last, we must declare its prototype before the show1() method.
Let’s update the code to remove this error. So, we have opened the file once more and added the function declaration of show2() after the namespace and before all methods.
After compiling the code, we have got a new error, although the previous error has been resolved. The error says that the code has an undefined reference. This means our code won’t work without the main() method. Also, we have not called the show1() method anywhere.
Thus, we have opened the file once again to fix this error. We have defined the main method at the last of both functions. The main method has been calling the show1() method. As the show1() method has been defined at the start, there is no need for function prototype declaration. The method show1() is calling the method show2(). This way, execution got completed.
Let’s just compile and run the code again. You can see that we have found no errors, and it works fine.
$ . /a.out
Conclusion:
In this guide, we have used simple examples for making situations to get the error: “function not declared in the scope”. Also, we have deliberated the techniques to resolve such issues with different methods. We hope you like this article and find it helpful.