#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) 上,我得到的错误是 this one , 虽然它不适用于 int 'cache[]'
.
有什么原因吗?
最佳答案
问题都是因为这里的第二行:
#include <algorithm>
using namespace std;
行using namespace std
带来来自 <algorithm>
的所有名称它还有一个名为 count
的函数,并且在您的代码中,您已经声明了一个变量 count
.因此出现了模棱两可的错误。
解决办法是从不写using namespace std
.很糟糕很糟糕。
改为使用 std::cout
, std::cin
, std::endl
, std::count
等等,在你的代码中。
关于c++ - 全局变量 "count"不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11271889/