2011-01-13 247 views
0

我在我的類中使用了一個3d維度tfpairexp作爲輸入參數的方法。我需要稍後使用tfpairexp中的值。將多維數組傳遞給函數

void calctfpairexp (int tf1, int tf2, double tfpairexp[][2][3]) 
{ 

    int ctr,c; 
    for (int j = 0; j < cchips && (c = chips[j].crepls); j += c) 
    { 
     int ctrl_no=0; 
     for (int *p = chips[j].ctrl ; p && (ctr=*p)>=0; ++p,ctrl_no++) { 

      for (int k = 0; k < c; ++k) 
      { 

       tfpairexp[j][ctrl_no][k]=interactionFunc(2,3,1); 
      } 
     } 
    } 

} 

我調用類中的方法是這樣的: calctfpairexp(tf1,tf2,tfpairexp); ,我需要在接下來的幾行裏面使用tfpairexp值。 但是編譯器給在這一行錯誤:

`calctfpairexp(tf1,tf2,tfpairexp);` 

它說,它無法找到calctfpairexp適當的合適的功能可按。任何想法?

+0

你有沒有考慮過使用'boost :: multi_array'這樣的類? – Philipp

+0

嗨。我應該如何使用它? – Pegah

回答

2

我期望從該函數聲明瞭語法錯誤,作爲數組PARAM聲明無效。您需要:

void calctfpairexp (int tf1, int tf2, double tfpairexp[][2][3]) 

數組索引的東西來帕拉姆聲明符

+0

謝謝你提到這個錯誤。是的,它解決了功能問題。但是在調用函數時仍然出現錯誤。它說它找不到合適的功能。 tfpairexp = new double [cchips] [max_ctrl_no] [max_rep_no]; – Pegah

+0

需要'雙(* tfpairexp)[2] [3] =新雙[芯片] [2] [3]' - 後尺寸必須是常數,並且必須符合函數期望什麼。否則,你會得到'沒有合適的功能'的錯誤。 –

0

而當使用這個原型?

void calctfpairexp (int tf1, int tf2, double *** tfpairexp) 

一個3維陣列的分配:

int w = 4; 
int h = 5; 
int d = 10; 
double *** ary = new double ** [w]; 
for(int i=0; i<w; ++i){ 
    ary[i] = new double * [h]; 
    for(int j=0; j<h; ++j){ 
     ary[i][j] = new double [d]; 
    } 
} 

取消分配:

for(int i=0; i<w; ++i){ 
    for(int j=0; j<h; ++j){ 
     delete [] ary[i][j]; 
    } 
    delete [] ary[i]; 
} 
delete [] ary; 
+0

謝謝。這種方式在calctfpairexp中沒有錯誤。但在這裏:double tfpairexp [cchips] [max_ctrl_no] [max_rep_no]; \t \t \t \t \t \t \t \t \t calctfpairexp(TF1,TF2,tfpairexp);它說沒有適合calctfpairexp的函數可以找到。 – Pegah

+0

您可以使用'new'或'malloc'動態分配該數組。 – tibur

+0

您已經完全改變了含義 - 將multidim數組更改爲指向單個dim數組指針數組的指針數組,儘管您可以使用相同的語法來訪問它,但它們並不完全相同。 –

0

使用參考後進行類型檢查大小:

void calctfpairexp (int tf1, int tf2, double (&tfpairexp)[][2][3]); 
0

我做了GCC快速測試,它並沒有給編譯器錯誤。由於我沒有源代碼的任何其他部分,我帶他們出去

double interactionFunc(int i, int j, int k) { return 0; } 
void calctfpairexp (int tf1, int tf2, double tfpairexp[][2][3]) 
{ 

    int ctr, c; 
    for (int j = 0; j < ctr; j += c) 
    { 
     int ctrl_no=0; 
     for (int k = 0; k < c; ++k, ctrl_no++) 
     { 

      tfpairexp[j][ctrl_no][k]=interactionFunc(2,3,1); 
     } 
    } 
} 

int main() 
{ 
    double d[1][2][3]; 
    calctfpairexp(1, 2, d); 
    return 0; 
} 

編譯結果:

[email protected]:~/Desktop$ g++ q1.cpp 
[email protected]:~/Desktop$ g++ --version 
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 

沒有錯。我不認爲這是導致問題的聲明。你可能想看看其他地方。