gpt4 book ai didi

c++ - error() 函数在哪个库中?

转载 作者:太空宇宙 更新时间:2023-11-04 16:03:42 25 4
gpt4 key购买 nike

我尝试在我的程序中使用 error() 函数,但它显示“错误:标识符“错误”未定义”。我需要包含一个库吗?这是从一本名为“Programming Principles and Practice Using C++”的书中找到的。 error() 函数将终止程序,系统错误消息加上作为参数传递给 error() 的字符串。

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.

最佳答案

这是一个用户定义的函数。如果你看一下 5.6.3 节“Bad input”,你可以看到,他将其定义为:

void error (string s)

{
throw runtime_error (s);
}

他在文本中进一步重新定义它以接受两个参数。

关于c++ - error() 函数在哪个库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com