Understanding 'Exit Was Not Declared In This Scope' Error in C++

For C++ developers, understanding and resolving C++ compilation errors can be difficult. One such error is the ‘exit was not declared in this scope’ error. This error message indicates that a program that includes the  header cannot find a declaration for the exit() function. In this documentation, we will explain what causes this error and the best ways to resolve the issue.

Error Causes

This error occurs due to the following reason:

  • The program isn’t including the proper header file.

The exit() function is declared in the  header, which contains several standard forms of utility functions, such as memory allocation and random number generation. This header must be included in the program for the exit() function to be used.

Solutions

The solution to this error is to include the appropriate header file. To include the  header, add the following code at the beginning of your program:

#include <cstdlib>

Once the header file has been added, the code will compile without errors. The problem should now be resolved.

FAQ

Q: What is the exit() function?

A: The exit() function is a standard C++ library function that terminates the program.

Q: What header file do I need to include for the exit() function?

A: The exit() function is declared in the  header. This header must be included for the exit() function to be used.

Conclusion

In short, the ‘exit was not declared in this scope’ error can be caused by not including the  header in the program. To resolve this issue, you must ensure that the header is included in the program. After doing so, the problem should be solved and the code should compile without errors.

Sources

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.