0

我有一门课,我已经得到了一些错误输入处理。这是一个被赋予了它自己的“提取”运算符的类,我被要求实现给我的代码。我遇到的问题是我应该使用的代码与此类似。

try {
    while (some condition)
    {....implemented code....}
} catch (runtime_error& e) {
    cerr << e.what() << endl;
    return 1;
}

我编译这个的问题是它似乎不喜欢“return 1”值,它给了我一个错误:

invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘int’

如果我删除返回值,它会直接编译,但程序一旦到达它试图执行 try 语句的区域,就无法执行。如前所述,我在上面的代码是我们应该实现的示例代码,所以我假设它直接开箱即用。我的 while 循环条件是

while (!std::cin.fail())

正如我假设的那样,我想继续获得输入,直到由于某种原因失败。为什么这种情况下的返回值会导致问题?

4

1 回答 1

1

心理调试说明:

您的封闭函数具有表单的签名

std::istream& func_name(/*parameter list goes here*/)

因此编译错误

于 2013-04-18T00:12:38.887 回答