簡體   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;
}

為什么我收到錯誤,即 i,j,h,w,dp 未在此范圍內聲明(在 sol 函數內)。如果我從代碼中刪除 dp[][] 數組,那么它運行時不會出現任何錯誤https:// ideone.com/uqz3p3

錯誤截圖

在 C++ 中,數組邊界必須是編譯時常量。 在您的代碼中int dp[][w] w是一個變量,而不是一個常數。

由於您已經在使用向量,我建議您也為dp使用向量。 main

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

sol

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM