-2

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.

A6EE
  • 141
  • 1
  • 8
  • 2
    Read the error message again, it tells you exactly what constructor is needed. – Some programmer dude Nov 21 '15 at 12:12
  • @JoachimPileborg Could you please review the question agian? I edited it. – A6EE Nov 21 '15 at 12:25
  • 2
    And you *still* need to read the error message, it still tells you exactly what's wrong. – Some programmer dude Nov 21 '15 at 12:29
  • This code is unnecessarily complicated contains several errors. The best thing to do now is *simplify* it until there is only one error and it is easy to solve; the best thing to do in the future is write code by building up slowly from the simple to the complex, so that you don't get syndromes like this. – Beta Nov 21 '15 at 12:47
  • I edited the question, could you please review it? – A6EE Nov 21 '15 at 12:59

1 Answers1

0

While I do agree with others that the error you are getting explains your situation, I do not agree with the harsh downvoting. The actual problem here seems to be that you want to initialize and array of objects using non default constructor.

Unforturnately, there is no straightforward way to do this. Take a look at this answer.

Your example with

Matrix<Complex<int>> **niz=new Matrix<Complex<int>>*[nr];

is one way to overcome this problem. The other is to use vector.

Community
  • 1
  • 1
Transcendental
  • 983
  • 2
  • 11
  • 27