2017-11-08 9 views
-1

私が行列をこのように宣言している:動的行列を関数に呼び出す方法は?

double **MB; 
    MB = new double *[650000]; 
    for (int count = 0; count < 650000; count++) 
    MB[count] = new double [2]; 

は今、私はそれを修正する必要がある関数で私の行列を呼びたいです。

bool notindic (..., double MB [][2], ...) {} 

とメインで:

notindic(..., MB, ...) 

今では、このエラーは私を与える:* [エラー]引数に '[2]ダブル()' から 'ダブル**' を変換することはできません " 3' BOOL notindic(STD ::文字列、のstd ::文字列、ダブル()[2]、int型、int型) '

に' 私はそれを修正できますか?

ありがとうございます。

+0

動的に割り当てられた配列と静的配列を混合しています。 MBが配列であることを示すためにコンパイラが利用できる情報はありません。それが懸念される限り、それは単なる二重**です。だからあなたはダブル**パラメータを受け入れるようにnotindicを変更します。 – Madhusudhan

答えて

0

だけ

#include <iostream> 


const int NX = 65; 
const int NY = 2; 

bool notindic(double** MB) { 
     for(int i = 0; i < NX; ++i) { 
       for(int j = 0; j < NY; ++j) { 
         MB[i][j] = i + j; 
       } 
     } 
} 

int main() { 
     double **MB = new double *[650000]; 
     for (int count = 0; count < 650000; count++) { 
       MB[count] = new double [2]; 
     } 

     notindic(MB); 

     for(int i = 0; i < NX; ++i) { 
       for(int j = 0; j < NY; ++j) { 
         std::cout << MB[i][j] << " "; 
       } 
       std::cout << std::endl; 
     } 
} 
0

はすべてそのポインタのナンセンスを忘れるパラメータとして配列ポインタを渡します。エラーが発生しやすく、例外的で安全でなく、書き込みが難しく、読みにくく、保守が難しく、パフォーマンスが低い可能性があります。行列をstd::vector<double>とし、それに応じてオフセットを計算します。

bool notindic (std::vector<double> const& matrix, int m) { 
    // ... 
    auto const element = matrix[y * m + x]; 
    // ... 
} 

auto const m = 650000; 
auto const n = 2; 
std::vector<double> my_matrix(m * n); 
auto const result = notindic(my_matrix, m); 

あなたはそれに取り組んでいる一方で、このようなクラスでラップ:あなたがそれらを必要とする場合

template <class T> 
class Matrix final 
{ 
public: 
    Matrix(int m, int n) : 
     data(m * n), 
     m(m) 
    { 
    } 

    T& operator()(int x, int y) 
    { 
     return data[y * m + x]; 
    } 

    T const& operator()(int x, int y) const 
    { 
     return data[y * m + x]; 
    } 

private: 
    std::vector<T> data; 
    int m; 
}; 

bool notindic (Matrix<double> const& matrix) { 
    // ... 
    auto const element = matrix(x, y); 
    // ... 
} 

auto const m = 650000; 
auto const n = 2; 
Matrix<double> my_matrix(m, n); 
auto const result = notindic(my_matrix); 

は、追加のメンバ関数を追加します。

関連する問題