2016-06-11 61 views
0

我有一個函數「mod_kadane」在類「MAX_SUB_MAT」中定義,它返回一個對象。C++:「沒有匹配的函數調用」功能錯誤

#define dx 101 
class MAX_SUB_MAT 
{ 


public: 
int curr_sum, max_sum, left, right, up, down; 

MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column) 
{ 
    MAX_SUB_MAT objx; 
    curr_sum = objx.max_sum = INT_MIN; 
    int sub_mat[row]; 

    for(int L = 0; L < row; L++) 
    { 
     memset(sub_mat, 0, sizeof sub_mat); 
     for(int R = L; R < column; R++) 
     { 
      for(int i = 0; i < row; i++) 
      { 
       sub_mat[i] += i;//mat[i][R]; 
      } 

      MAX_SUB_ARR obj; 
      obj = obj.kadane(sub_mat, row); 
      curr_sum = obj.maxima; 

      if(curr_sum > objx.max_sum) 
      { 
       objx.max_sum = curr_sum; 

       objx.left = L; 
       objx.right = R; 
       objx.up = obj.left; 
       objx.down = obj.right; 
      } 
     } 
    } 
    return objx; 
} 
}; 

但是,當我試圖從「主」稱呼它:

cin >> row >> column; 
int mat[row][column]; 
for(int i = 0; i < row; i++) 
    { 
     for(int j = 0; j < column; j++) 
     { 
      cin >> mat[i][j]; 
     } 
    } 

MAX_SUB_MAT objx; 
objx = objx.mod_kadane(mat, row, column); 

則顯示錯誤:

error: no matching function for call to 'MAX_SUB_MAT::mod_kadane(int [row][column], int&, int&)'|

但如果我刪除了二維數組「墊「從雙方那麼代碼工作正常。

這裏是我的全碼:

#include<bits/stdc++.h> 

#define dx 101 

using namespace std; 

class MAX_SUB_ARR 
{ 
public: 
int max_curr, maxima, left, right; 


MAX_SUB_ARR kadane(int arr[], int n) 
{ 
    MAX_SUB_ARR obj; 

    obj.maxima = max_curr = INT_MIN; 
    //cout << obj.maxima << endl; 
    /*for(int i = 0; i < n; i++) 
     cout << arr[i] << endl;*/ 
    for(int i = 0; i < n; i++) 
    { 
     if(max_curr < 0) 
     { 
      max_curr = arr[i]; 
      obj.left = i; 
     } 
     else 
     { 
      max_curr += arr[i]; 
     } 

     //maxima = max(maxima, max_curr); 
     if(max_curr > obj.maxima) 
     { 
      obj.maxima = max_curr; 
      obj.right = i; 
      //cout << obj.maxima << endl; 
     } 
    } 

    return obj; 
} 
}; 

class MAX_SUB_MAT 
{ 


public: 
int curr_sum, max_sum, left, right, up, down; 
/* MAX_SUB_MAT(int r, int c) 
{ 
    row = r; 
    column = c; 
}*/ 

MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column) 
{ 
    MAX_SUB_MAT objx; 
    curr_sum = objx.max_sum = INT_MIN; 
    int sub_mat[row]; 

    for(int L = 0; L < row; L++) 
    { 
     memset(sub_mat, 0, sizeof sub_mat); 
     for(int R = L; R < column; R++) 
     { 
      for(int i = 0; i < row; i++) 
      { 
       sub_mat[i] += i;//mat[i][R]; 
      } 

      MAX_SUB_ARR obj; 
      obj = obj.kadane(sub_mat, row); 
      curr_sum = obj.maxima; 

      if(curr_sum > objx.max_sum) 
      { 
       objx.max_sum = curr_sum; 

       objx.left = L; 
       objx.right = R; 
       objx.up = obj.left; 
       objx.down = obj.right; 
      } 
     } 
    } 
    return objx; 
} 
}; 


int main() 
{ 

int row, column; 

printf("Number of rows you want to insert?:\n"); 
cin >> row; 

printf("Number of columns you want to insert?:\n"); 
cin >> column; 

int mat[row][column]; 

if(row && column) 
{ 
    for(int i = 0; i < row; i++) 
    { 
     for(int j = 0; j < column; j++) 
     { 
      cin >> mat[i][j]; 
     } 
    } 

    //int curr_sum, max_sum, left, right, up, down; 


    //MAX_SUB_MAT objx = new MAX_SUB_MAT(row, column); 
    MAX_SUB_MAT objx; 
    objx = objx.mod_kadane(mat, row, column); 

    for(int i = objx.up; i <= objx.down; i++) 
    { 
     for(int j = objx.left; j <= objx.right; j++) 
     { 
      cout << mat[i][j] << " "; 
     } 

     cout << endl; 
    } 
} 

return 0; 
} 
+0

看看http://stackoverflow.com/questions/9446707/correct-way-of-passing-2-dimensional-array-into-a-function – sg7

+3

'int mat [row] [column]'isn在C++中有效,因爲維度不是常量。它絕對不同於你的參數被聲明爲的int mat [101] [101]'。 –

+0

@AlanStokes謝謝。但我不明白爲什麼維度需要保持不變。 2d數組是一個不是全局的局部變量。是因爲作爲參數傳遞給函數嗎? –

回答

0

這是不正確的,在C或C++中,我們不能宣佈一個函數與指定的兩種尺寸二維數組參數。

請參見:http://c-faq.com/aryptr/pass2dary.html

問題已經正確地@AlanStokes在他的評論解釋!

+2

是的,你可以(https://ideone.com/PmLiGZ)。這是完全有效的,儘管可能會誤導,因爲實際上忽略了第一個邊界。 –

+0

這裏的參數聲明'int mat [dx] [dx]'與'int mat [] [dx]'完全等價,這是FAQ條目的推薦之一。 –

+0

@AlanStokes我同意。我的聲明「我們不能用兩個數組參數聲明一個具有指定大小的函數」是錯誤的!謝謝你指出! – sg7

相關問題