Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What library is the error() function in?

Tags:

c++

I try to use the error() function in my program, but it shows "Error: identifier "error" is undefined". Is there a library that I need to include? This is found from a book called "Programming Principles and Practice Using C++". The error() function will terminate the program with a system error message plus the string that is passed to error() as an argument.

if (x<=0) error("non-positive x"); 
if (y<=0) error("non-positive y");
int area1 = area(x,y); 

Really, the only question is what to do if we find an error. Here, we have called a function error() which we assume will do something sensible. In fact, in std_lib_facilities.h we supply an error() function that by default terminates the program with a system error message plus the string we passed as an argument to error(). If you prefer to write out your own error message or take other actions, you catch runtime_error (§5.6.2, §7.3, §7.8, §B.2.1). This approach suffices for most student programs and is an example of a style that can be used for more sophisticated error handling.

like image 513
Herman Tam Avatar asked Mar 01 '26 08:03

Herman Tam


2 Answers

It is a user defined function. If you take a look at section 5.6.3 "Bad input", you can see, he defines it as:

void error (string s)

{
  throw runtime_error (s);
}

He redefines it further in the text to accept two arguments.

like image 191
mvanlint Avatar answered Mar 02 '26 21:03

mvanlint


In your book

Support

The book's support website, http://www.stroustrup.com/Programming, contains a variety of materials supporting the teaching and learning of programming using this book. The material is likely to be improved with time, but for starters, you can find:

  • Slides for lectures based on the book
  • An instructor's guide
  • Header flies and implementations of libraries used in the book
  • Code for examples in the book
  • Solutions to selected exercises
  • Potentially useful links
  • Errata Suggestions for improvements are always welcome.

And later at the end of first chapter:

So, here is your first drill:

  1. Go to Appendix C and follow the steps required to set up a project. Set up an empty, console C++ project called hello_ world.

[...]

How do you find std_lib_facilities.h? If you are in a course, ask your instructor. If not, download it from our support site http://www.stroustrup.com/Programming.

like image 26
Revolver_Ocelot Avatar answered Mar 02 '26 20:03

Revolver_Ocelot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!