0
#include <iostream>
#include <math.h>
using namespace std;

template <typename type>
class Vect
{
public:
    type *vect; // array vect
    int vectSize = 0;
public:

    void setVect(type *x, int n, int m) {
        cout << "n : " << n << endl;
        cout << "m : " << m << endl;
        int mem = n * m;
        vect = new type[mem];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << " vect : " << *(x + i * m + j) << endl;
                vectSize++;
            }
        }
    }
};



template <typename type>
class Matr
{
public:
    int n, m;
    int sizeVect;
    int maxSizeVect = 3;
    type** x;
    type p;
    
    Vect<type> **vectorMas; // arr objects vector

Matr(int n1, int m1, double w1, double w2) {
        n = n1;
        m = m1;
        x = new type * [n];
        for (int i = 0; i < n; i++) {
            x[i] = new type[m];
            for (int j = 0; j < m; j++) {
                x[i][j] = w1 * (j + 1) * sin( (i + 1) / 2) - w2 * (i + 1) * cos( (j + 1) / 3);
            }
        }
        vectorMas = new Vect<type> * [maxSizeVect];
        for (int i = 0; i < maxSizeVect; i++) { vectorMas[i] = NULL; }
        add_vectMas();
    }

void Add_Dobmemory(){
 Vect<type>** buff;
        buff = new Vect<type> * [sizeVect];

        for (int i = 0; i < sizeVect; i++) {
            buff[i] = vectorMas[i];
        }

        delete[] buff;
        maxSizeVect *= 2;
}

void add_vectMas() {
if (sizeVect >= maxSizeVect) {
            Add_Dobmemory();
        }

        vectorMas[sizeVect] = new Vect<type>();
        vectorMas[sizeVect]->setVect(*x, n, m);
        sizeVect++;

};

void print_m() {
        cout << "-------------------------------";
        cout.width(10);
        cout.precision(3);
        for (int i = 0; i < n; i++) {
            cout << endl;
            for (int j = 0; j < m; j++) {
                cout.width(6);
                cout << x[i][j] << " ";
            }
        }
        cout << endl;
    }

};

int main()
{
   setlocale(LC_ALL, "Russian");
   Matr<double> matr(4, 4, 5.7, 9.3);
   matr.print_m();

}

when in function add_vectMas() I create a vector object and pass the matrix - x to its setVect(*x) method, the matrix breaks. How can I pass the matrix - x correctly? so that I get the correct values ​​inside this method.

Output: Matr----------------------- 9.3 9.3 5.02 5.02 13.8 -9.01 4.34 9.14 23.1 -18.3 -0.685 4.11 32 26.8 4.55 0.633 n : 4 m : 4

matr x in Vect-> setVect(type *x, int n, int m) {

i: 0 vect : 9.3 i: 1 vect : 9.3 i: 2 vect : 5.02 i: 3 vect : 5.02 i: 4 vect : 2.11e-314 i: 5 vect : -7.01e-251 i: 6 vect : 1.1e-311 i: 7 vect : 1.1e-311 i: 8 vect : 0 i: 9 vect : 2.12e-314 i: 10 vect : 1.58e-322 i: 11 vect : -7.85e+298 i: 12 vect : 1.1e-311 i: 13 vect : 1.1e-311 i: 14 vect : 1.1e-311 i: 15 vect : 1.1e-311

Cooler-cpu
  • 31
  • 1
  • 7
  • The main problem seems to be that you think that `x` is contiguous like an array of arrays, while it's not. `x` is a [*jagged array*](https://en.wikipedia.org/wiki/Jagged_array) and could be seen as an array of *pointers* and not an array of arrays. Therefore the data in it are *not* contiguous the way you expect. See e.g. [this old answer of mine](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456) for an illustration of it. – Some programmer dude Feb 15 '21 at 06:36
  • I understand, but I can't figure out how can I access this matrix? – Cooler-cpu Feb 15 '21 at 06:56
  • In my case the question boils down to that. How to correctly pass a two dimensional array x to a method of another class. – Cooler-cpu Feb 15 '21 at 07:57
  • @Ted Lyngmo now made it compileable – Cooler-cpu Feb 15 '21 at 08:17
  • 1
    Pass the pointer to pointer, and access it as an array of arrays? I.e. `void setVect(type **x, int n, int m` and `x[i][j]` – Some programmer dude Feb 15 '21 at 08:18
  • @Cooler-cpu Great. [You now see where it crashes](https://godbolt.org/z/ndMcof) – Ted Lyngmo Feb 15 '21 at 08:18
  • @Some programmer dude vectorMas[sizeVect]->setVect(*x, n, m); one pointer doesn't work if void setVect(type ** x, int n, int m) and x [i] [j] – Cooler-cpu Feb 15 '21 at 08:27
  • @Some programmer dude it works if you do as you said setVect (type ** x, int n, int m) and x [i] [j] and pass vectorMas [sizeVect] -> setVect (x, n, m); matrix without pointer – Cooler-cpu Feb 15 '21 at 08:31

0 Answers0