#include <algorithm>
using namespace std;
int count = 0, cache[50];
int f(int n)
{
if(n == 2) count++;
if(n == 0 || n==1) return n;
else if (cache[n] !=- 1) return cache[n];
else cache[n]= f(n-1) + f(n-2);
return cache[n];
}
我在 gcc 4.3.4 中使用了这个函数,得到了以下错误:
prog.cpp: In function ‘int f(int)’:
prog.cpp:38: error: reference to ‘count’ is ambiguous
在我的本地机器(mingw32)上,我得到的错误是 这个,虽然它不是 int 'cache[]'
。
有什么理由吗?
原文由 user801154 发布,翻译遵循 CC BY-SA 4.0 许可协议
问题都是因为这里的第二行:
The line
using namespace std
brings all the names from<algorithm>
which also has a function calledcount
, and in your code, you’ve declared a variablecount
.因此,模棱两可的错误。解决方案是 永远不要 写
using namespace std
。很糟糕很糟糕。相反,请在您的代码中使用
std::cout
,std::cin
,std::endl
,std::count
和so onbaf