First off, here is the code and then I'll explain the problem:
// Matrix.h
#pragma once
#include <iostream>
#include <iomanip>
using namespace std;
template <typename t>
class Matrix
{
int n, m; // nxm dimension
char name; // name of matrix
t **data; // data
public:
Matrix(int, int, char, t); // constructor for initialization of all (int x int size) matrix elements with value of type t
Matrix(Matrix<t> &); //copy constructor which probably is the source of the problem here
Matrix(int);
~Matrix();
Matrix<t> operator=(const Matrix<t> &);
};
template<typename t> Matrix<t>::Matrix(int n = 1, int m = 1, char p='n', t data = 0)
{
this->n = n;
this->m = m;
this->name = p;
this->data = new t*[this->n];
for (int i = 0;i < n;++i)
{
this->data[i] = new t[this->m];
for (int j = 0;j < m;++j)
{
this->data[i][j] = data;
}
}
}
template<typename t> Matrix<t>::Matrix(Matrix<t> &m)
{
this->n = m.n;
this->m = m.m;
this->name=m.name;
this->data = new t*[this->n];
for (int i = 0;i < n;++i)
{
this->data[i] = new t[this->m];
for (int j = 0;j < this->m;++j)
{
this->data[i][j] = m.data[i][j];
}
}
}
template<typename t> Matrix<t>::Matrix(int n)
{
this->n = n;
this->m = n;
this->name = 't';
this->data = new t*[this->n];
for (int i = 0;i < n;++i)
{
this->data[i] = new t[this->m];
for (int j = 0;j < this->m;++j)
{
this->data[i][j] = m.data[i][j];
}
}
}
template<typename t> Matrix<t>::~Matrix()
{
for (int i = 0;i < this->n;++i)
delete[] data[i];
delete[] data;
}
template<typename t> Matrix<t> Matrix<t>::operator=(const Matrix<t> &x)
{
if (this != &x)
{
if (n != x.n)
{
n = x.n;
data = new t*[n];
}
if (m != x.m)
{
m = x.m;
for (int i = 0;i < m;++i)
data[i] = new t[m];
}
for (int i = 0; i < n; ++i)
for (int j = 0;j < m;++j)
{
data[i][j] = x.data[i][j];
}
}
return *this;
}
//main.cpp
#include "Matrix.h"
int main()
{
int nr, n;
cout<<"Number of matrices: ";
cin>>nr;
cout<<"Dimension: ";
cin>>n;
Matrix<Complex<int>> *niz=new Matrix<Complex<int>>(n)[nr]; // this is where I get an error: "no suitable conversion function from "Matrix<Complex<int>>" to "Matrix<Complex<int>> * exists"
What constructor should I make so that I can make array of matrices? When I try to make array of pointers to matrices like this:
Matrix<Complex<int>> **niz=new Matrix<Complex<int>>*[nr];
for(int i=0;i<br;++i)
{
niz[i] = new Matrix<Complex<int>>(n, n, 't', 0);
}
It all goes well and I can do all the operations with pointers, but, I'm actually pretty confused.
EDIT:
This is how I came around the problem, it works, but just looks wrong. How can I create a constructor which will do all this:
1) I added a new method to Matrix class:
void setNM(int n, int m)
{
for (int i = 0;i < this->n;++i)
delete[] data[i];
delete[] data;
this->n = n;
this->m = m;
this->name = 't';
this->data = new t*[this->n];
for (int i = 0;i < n;++i)
{
this->data[i] = new t[this->m];
for (int j = 0;j < m;++j)
{
this->data[i][j] = 0;
}
}
}
2) Then, when I declare an array of matrices like this:
Matrix<Complex<int>> *niz = new Matrix<Complex<int>>[nr];
It creates an array of nr matrices sized 1x1;
Then, when I call:
niz[0].setNM(n,n);
It basically destroys the old matrix and creates a new one sized nxn. How can I do all this with this call:
Matrix<Complex<int>> *niz = new Matrix<Complex<int>>(n,n)[nr];
I don't know what constructor to make.