简体   繁体   English

C++ 构造函数错误:矩阵类,无法初始化矩阵数组

[英]C++ constructor error: Matrix Class, cannot initialize array of matrices

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: 1)我向 Matrix 类添加了一个新方法:

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: 2)然后,当我像这样声明一个矩阵数组时:

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

It creates an array of nr matrices sized 1x1;它创建了一个大小为 1x1 的 nr 矩阵数组;

Then, when I call:然后,当我打电话时:

niz[0].setNM(n,n);

It basically destroys the old matrix and creates a new one sized nxn.它基本上破坏了旧矩阵并创建了一个新的 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.我不知道要制作什么构造函数。

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 .另一种是使用vector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM