Eclipse C++ 不允许我使用全局变量?
Eclipse c++ not letting me use global variable?
我试图让这个递归程序计算它调用自身的次数,我打算使用一个全局变量来保持计数,但 Eclipse 出于某种原因无法识别它。这是我的代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int count = 0;
int fib(long int);
int main()
{
long int number;
cout << "Enter a number => ";
cin >> number;
cout << "\nAnswer is: " << fib(number) << endl;
return 0;
}
int fib (long int n)
{
//cout << "Fibonacci called with: " << num << endl;
if ( n <0 )
{
cout <<" error Invalid number\n";
exit(1);
}
else if (n == 0 || n == 1)
return 1;
else{
count++;
return fib(n-1) + fib(n-2);}
cout << count;
}
每当我最初声明 count 时,它甚至不将其识别为变量,有人知道这是什么原因吗?
您的问题在这里:
using namespace std;
引入了std::count
from the algorithm header,所以现在count
是有歧义的。这就是为什么人们被告知不要做 using namespace std;
。相反,删除该行并放置 std::cout
而不是 cout
(cin
和 endl
也是如此)。
我试图让这个递归程序计算它调用自身的次数,我打算使用一个全局变量来保持计数,但 Eclipse 出于某种原因无法识别它。这是我的代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int count = 0;
int fib(long int);
int main()
{
long int number;
cout << "Enter a number => ";
cin >> number;
cout << "\nAnswer is: " << fib(number) << endl;
return 0;
}
int fib (long int n)
{
//cout << "Fibonacci called with: " << num << endl;
if ( n <0 )
{
cout <<" error Invalid number\n";
exit(1);
}
else if (n == 0 || n == 1)
return 1;
else{
count++;
return fib(n-1) + fib(n-2);}
cout << count;
}
每当我最初声明 count 时,它甚至不将其识别为变量,有人知道这是什么原因吗?
您的问题在这里:
using namespace std;
引入了std::count
from the algorithm header,所以现在count
是有歧义的。这就是为什么人们被告知不要做 using namespace std;
。相反,删除该行并放置 std::cout
而不是 cout
(cin
和 endl
也是如此)。