2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Bug - Multiple Definitions
Your problem is that you are getting a "reference to 'data' is ambiguous" error in your C++ code. This error usually occurs when you try to reference a variable that has multiple definitions.
In your code, you define a global variable data, and there is also a function template named data in the C++ standard library. When you use data in the fib function, the compiler cannot determine whether you want to refer to the global variable data or std::data, so it gives an ambiguous error.
To solve this problem, you can change the name of the global variable data, or explicitly specify that you are referring to the global variable when calling.
Solution 1: Change the global variable name
cpp
#include <iostream>
using namespace std;
int mod=1e9+7;
int fibData[10000]; // Change the name
int fib(int n){
if(n==1||n==2){fibData[n]=1;return fibData[n];}
if(fibData[n]!=0)return fibData[n];
fibData[n]=fibData[n-1]+fibData[n-2];
return fibData[n];
}
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++){
int n;
cin>>n;
cout<<fib[n];
}
return 0;
}
Solution 2: Specify that you want to refer to a global variable when calling
cpp
#include <iostream>
using namespace std;
int mod=1e9+7;
int data[10000];
int fib(int n){
if(n==1||n==2){::data[n]=1;return ::data[n];} // Add '::' in front to explicitly specify that you are referencing a global variable
if(::data[n]!=0)return ::data[n];
::data[n]=::data[n-1]+::data[n-2];
return ::data[n];
}
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++){
int n;
cin>>n;
cout<<fib[n];
}
return 0;
}