#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