简体   繁体   中英

Why i am getting error not declared in this scope?

#include<iostream>
#include<vector>
using namespace std;

int sol(int i,int j,vector<vector<char>>v,int h,int w,int dp[][w]){
    if(i>h || j>w){
        return 0;
    }
    if(i==h && j==w){
        return 1;
    }
    if(v[i][j]=='#'){
        return 0;
    }
    if(dp[i][j]!=-1){
        return dp[i][j];
    }
    dp[i][j]=sol(i+1,j,v,h,w,dp) + sol(i,j+1,v,h,w,dp);
    return dp[i][j];
}

int main(){
    int h,w;
    cin>>h>>w;
    vector<vector<char>>v(h);
    char c;
    int dp[h][w];
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c;
            v[i].push_back(c);
            dp[i][j]=-1;
        }
    }
    h--;
    w--;
    cout<<sol(0,0,v,h,w,dp)<<endl;
}

Why i am getting error that i,j,h,w,dp is not declared in this scope(Inside sol function).If i remove the dp[][] array from my code then it run without any error https://ideone.com/uqz3p3 )

Error Screenshot

In C++ array bounds must be compile time constants. In your code int dp[][w] w is a variable, not a constant.

Since you are already using vectors I suggest you use a vector for dp as well. In main

vector<vector<int>> dp(h, vector<int>(w));

and in sol

int sol(int i,int j,vector<vector<char>>v,int h,int w,vector<vector<int>>& dp) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM