【发布时间】:2017-09-16 19:13:19
【问题描述】:
我使用C++14 在 GCC 中编译了以下程序。
#include <iostream>
using namespace std;
auto func(int i);
int main()
{
auto ret = func(5);
return 0;
}
auto func(int i)
{
if (i == 1)
return i;
else
return func(i-1) + i;
}
但是,我收到以下错误。
In function 'int main()': 8:16: error: use of 'auto func(int)' before deduction of 'auto' auto ret = func(5);
那么,我在这里错过了什么?
【问题讨论】:
-
请记住,C++ 是为一次性编译器设计的(许多编译器仍然如此)。
ret的扣除取决于func,但在执行单遍翻译时尚不可用。 -
你认为应该给
func什么返回类型,为什么? -
@n.m.返回类型 int 因为 func() 返回 1 到 n 个数字的总和。
-
直到现在我才意识到您问题中的功能与标准中示例中的功能相同...这是巧合吗?因为如果不是,那么您只是错过了示例的第 2 行和第 3 行。 (是的,我确实意识到这是一个非常常见的递归方案。只是确保。)
-
糟糕,我的错误,请忽略。
标签: c++ function gcc c++14 auto