《C++编程原理与实践(第3版)》expect函数不存在问题
《C++编程原理与实践(第3版)》中
expect函数未定义问题 我有Java和基础C学习基础,目前在用Bjarne的《C编程原理与实践(第3版)》学习C++,学到第4章错误处理部分时,发现书中提到的expect函数在编译时无法识别。
示例代码
// try to find a pair of values such that the precondition holds but the postcondition doesn't import std; using namespace std; // calculate area of the rectangle int area(int length, int width) { // if the arguments are positive expect([&] {return 0 < length && 0 < width; }, "bad arguments for area()"); // line of error int result = length * width; expect([&] {return 0 < result; }, "bad area() result"); // line of error return result; } int main() { // simple test, infinite loop while (true) try { int length = 0, width = 0; cout << "Enter a length and a width: "; cin >> length >> width; cout << "Area is: " << area(length, width) << endl; } catch (...) { cerr << "An error occured" << endl; } return 0; }
编译错误
使用Visual Studio 2022的c++latest编译选项时,出现以下错误:
trythis04.cpp Z:\...\trythis04.cpp(9): error C3861: 'expect': identifier not found Z:\...\trythis04.cpp(11): error C3861: 'expect': identifier not found
疑问与求助
我在cppreference上只找到了语法完全不同的expected,书中说明expect接收测试函数和错误提示字符串。想请教:
- 这个问题是教材失误、我的操作有误还是标准后续修订导致的?
- 应该用什么函数或方式替代书中的
expect?
内容的提问来源于stack exchange,提问作者Xbox 360
相关产品推荐
相关产品推荐

