Reputation: 53
unusual error in function solve when I pass the dp array to the function.
int n,k;
int solve(int n, int k, int dp[n+1][k+1])
{
// some code
}
int main(){
int t; cin>>t;
while(t--){
cin>>n;
cin>>k;
int dp[n+1][k+1];
memset(dp, -1,sizeof(dp));
cout<<solve(n,k,dp)<<endl;
}
return 0;
}
why this error: use of parameter outside function body before '+' token int solve(int n, int k, int dp[n+1][k+1]) I am not able to understand why is this error
Upvotes: 0
Views: 103
Reputation: 3
In C++, the array's size can't be changed dynamically. Granted you are reading in the sizes, this will cause issues. You can hard code the value of n,k for testing purposes
int dp[10][10];
int solve(int n, int k, int dp[10][10])
or attempt using a vector instead.
Upvotes: 0
Reputation: 87959
In C++ array sizes must be compile time constants.
This is not legal C++
int dp[n+1][k+1];
because n
and k
are variables not constants.
And this is not legal C++
int solve(int n, int k, int dp[n+1][k+1])
for exactly the same reason.
Upvotes: 2