MainClass::MainClass(){ ... double EF[9]; double V[6]; ... long int kt1 = LGL / h1 + 1; double BL1[kt1 + 2][2]; ... INTdBds (LGL, h1, EF, &V, &BL1, &kt1 ); ...}void MainClass::INTdBds (double S, double h0, double NU, double *REZ, double *B, long int *i){ ... *i = 2; ... *B[*i-1][ 0] = x1[0]; *B[*i-1][ 1] = y1[0]; *REZ[0] = x1[0]; *REZ[1] = y1[0]; *REZ[2] = z1[0]; *REZ[3] = Zd[0];}
void MainClass::INTdBds(double S, double ho, double *NU, double *REZ, double **B, long int &i) ... i = 2; ... B[i-1][ 0] = x1[0]; B[i-1][ 1] = y1[0]; REZ[0] = x1[0]; REZ[1] = y1[0]; REZ[2] = z1[0]; REZ[3] = Zd[0];}
INTdBds (LGL, h1, EF, V, BL1, kt1 );
double *EF= new double[9];double *V=new double[6];long int kt1 = LGL / h1 + 1;double **BL1=new double[kt1 + 2];for(int j=0;j<kt1 + 2; ++j) BL1[j]=new double[2];
void MainClass::INTdBds(double S, double ho, double *NU, double *REZ, double **B, long int &i)
C++ (Qt)struct MYData { double EF[9]; double V[6]; int kt1; double * BL1;}; MyData data; data.kt1 = LGL / h1 + 1; data.BL1 = new double[(data.kt1 + 2) * 2]; ... INTdBds (LGL, h1, &data); ... delete [] data.BL1;
C++ (Qt)double * BL1 = new double[(kt1 + 2)*2];
C++ (Qt)BL1[0][0] = 1.0;
C++ (Qt)int rows = kt1 + 2;double **BL1 = new double *[rows];for (int i = 0; i < rows; ++i) BL1[i] = new double [2];
Оно и понятно. Массив создался одномерный. Как сделать двумерный?
C++ (Qt)int rows = 3;int columns = 3;double **Arr = new double*[rows]; for (int i = 0; i < rows; ++i) Arr[i] = new double[columns];
C++ (Qt)#ifndef ARRAY2D_H#define ARRAY2D_H #include <stdexcept> template <class T>class Array2D{public: typedef size_t size_type; Array2D(); Array2D(size_type rows, size_type columns, const T &initValue = T()); Array2D(const Array2D<T> &other); ~Array2D(); T &operator() (size_type row, size_type column) throw(std::out_of_range); const T &operator() (size_type row, size_type column) const throw(std::out_of_range); Array2D &operator=(const Array2D<T> &array); void resize(size_type rows, size_type columns); bool swapRows(size_type i, size_type j); size_type columns() const { return m_columns; } size_type rows() const { return m_rows; }private: T **m_arr; size_type m_rows; size_type m_columns;};//----------------------------------------------------------------------------- template <class T>Array2D<T>::Array2D() : m_arr(0), m_rows(0), m_columns(0){}//----------------------------------------------------------------------------- template <class T>Array2D<T>::Array2D(size_type rows, size_type columns, const T &initValue) : m_rows(rows), m_columns(columns){ m_arr = new T*[m_rows]; for (size_type i = 0; i < m_rows; ++i) m_arr[i] = new T[m_columns]; for (size_type i = 0; i < m_rows; ++i) { for (size_type j = 0; j < m_columns; ++j) { m_arr[i][j] = initValue; } }}//----------------------------------------------------------------------------- template <class T>Array2D<T>::Array2D(const Array2D<T> &other){ m_rows = other.rows(); m_columns = other.columns(); m_arr = new T*[m_rows]; for (size_type i = 0; i < m_rows; ++i) m_arr[i] = new T[m_columns]; for (size_type i = 0; i < m_rows; ++i) { for (size_type j = 0; j < m_columns; ++j) { m_arr[i][j] = other(i, j); } }}//----------------------------------------------------------------------------- template <class T>Array2D<T>::~Array2D(){ for (size_type i = 0; i < m_rows; ++i) delete []m_arr[i]; delete []m_arr;}//----------------------------------------------------------------------------- template <class T>Array2D<T> &Array2D<T>::operator=(const Array2D<T> &array){ if (this == &array) { return *this; } if (m_rows != array.rows() || m_columns != array.columns()) { for (size_type i = 0; i < m_rows; ++i) delete []m_arr[i]; delete []m_arr; m_rows = array.rows(); m_columns = array.columns(); m_arr = new T*[m_rows]; for (size_type i = 0; i < m_rows; ++i) m_arr[i] = new T[m_columns]; } for (size_type i = 0; i < m_rows; ++i) { for (size_type j = 0; j < m_columns; ++j) { m_arr[i][j] = array(i, j); } } return *this;}//----------------------------------------------------------------------------- template <class T>T &Array2D<T>::operator ()(size_type row, size_type column) throw(std::out_of_range){ if ((row >= m_rows) || (column >= m_columns)) throw std::out_of_range("Array2D: out of range!"); return m_arr[row][column];}//----------------------------------------------------------------------------- template <class T>const T &Array2D<T>::operator ()(size_type row, size_type column) const throw(std::out_of_range){ if ((row >= m_rows) || (column >= m_columns)) throw std::out_of_range("Array2D: out of range!"); return m_arr[row][column];}//----------------------------------------------------------------------------- template <class T>void Array2D<T>::resize(size_type rows, size_type columns){ if (rows != m_rows || columns != m_columns) { T **newArr = new T*[rows]; for (size_type i = 0; i < rows; ++i) newArr[i] = new T[columns]; for (size_type i = 0; i < rows; ++i) { for (size_type j = 0; j < columns; ++j) { newArr[i][j] = T(); } } size_type r = (m_rows > rows) ? rows : m_rows; size_type c = (m_columns > columns) ? columns : m_columns; for (size_type i = 0; i < r; ++i) { for (size_type j = 0; j < c; ++j) { newArr[i][j] = m_arr[i][j]; } } for (size_type i = 0; i < m_rows; ++i) delete []m_arr[i]; delete []m_arr; m_arr = newArr; m_columns = columns; m_rows = rows; }}//----------------------------------------------------------------------------- template <class T>bool Array2D<T>::swapRows(size_type i, size_type j){ if ((i >= m_rows) || (j >= m_rows)) return false; if (i == j) return true; T *a = m_arr[i]; m_arr[i] = m_arr[j]; m_arr[j] = a; return true;}//----------------------------------------------------------------------------- #endif // ARRAY2D_H