简体   繁体   English

为什么我在这个 scope 中没有声明错误?

[英]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 )为什么我收到错误,即 i,j,h,w,dp 未在此范围内声明(在 sol 函数内)。如果我从代码中删除 dp[][] 数组,那么它运行时不会出现任何错误https:// ideone.com/uqz3p3

Error Screenshot错误截图

In C++ array bounds must be compile time constants.在 C++ 中,数组边界必须是编译时常量。 In your code int dp[][w] w is a variable, not a constant.在您的代码中int dp[][w] w是一个变量,而不是一个常数。

Since you are already using vectors I suggest you use a vector for dp as well.由于您已经在使用向量,我建议您也为dp使用向量。 In mainmain

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

and in solsol

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到“未在此范围内声明”错误? - why am I getting a “was not declared in this scope” error? 为什么我没有在此 scope 错误中声明 - Why am I getting not declared in this scope error 为什么我在声明本身中收到“未在范围内声明”错误? - Why am I getting a "not declared in scope" error in the declaration itself? 为什么我收到“未在该范围内声明getuid”错误? - Why I am getting “getuid was not declared in that scope” error? 当我包含windows.h时,为什么会出现错误“在此范围内未声明WM_MENUCOMMAND”? - Why am I getting the error “WM_MENUCOMMAND was not declared in this scope” when I included windows.h? 为什么我会收到编译错误“ <variable> 没有在此范围内声明”? - Why am I getting compile errors “<variable> not declared in this scope”? 我收到“错误:字符串未在此范围内声明” - I'm getting an "Error: string was not declared in this scope" 获取“未在此范围内声明”错误 - Getting a 'was not declared in this scope' error 无法实例化好友函数中的类? 我没有在范围错误中声明 - Not able to Instantiate class inside friend function? I am getting not declared in scope error 我在 c++ 中遇到错误“PTHREAD_START_ROUTINE”未在此范围内声明 - I am getting error in c++ 'PTHREAD_START_ROUTINE' was not declared in this scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM