C++ constructor error: Matrix Class, cannot initialize array of matrices
C++ constructor error: Matrix Class, cannot initialize array of matrices
首先,这是代码,然后我将解释问题:
// 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"
我应该制作什么构造函数才能制作矩阵数组?当我尝试像这样制作指向矩阵的指针数组时:
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);
}
一切顺利,我可以用指针做所有的操作,但是,我其实很困惑。
编辑:
这就是我解决问题的方法,它有效,但看起来不对。我怎样才能创建一个构造函数来完成所有这些:
1) 我给 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) 然后,当我像这样声明一个矩阵数组时:
Matrix<Complex<int>> *niz = new Matrix<Complex<int>>[nr];
它创建一个大小为 1x1 的 nr 矩阵数组;
然后,当我打电话时:
niz[0].setNM(n,n);
它基本上破坏了旧矩阵并创建了一个新的一个大小的 nxn。我怎样才能通过这个电话完成所有这些工作:
Matrix<Complex<int>> *niz = new Matrix<Complex<int>>(n,n)[nr];
我不知道要做什么构造函数。
虽然我同意其他人的观点,即您遇到的错误解释了您的情况,但我不同意严厉的否决票。这里的实际问题似乎是您想使用非默认构造函数初始化对象数组。
不幸的是,没有直接的方法可以做到这一点。看看this answer.
你的例子
Matrix<Complex<int>> **niz=new Matrix<Complex<int>>*[nr];
是解决这个问题的一种方法。另一种是使用vector.
首先,这是代码,然后我将解释问题:
// 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"
我应该制作什么构造函数才能制作矩阵数组?当我尝试像这样制作指向矩阵的指针数组时:
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);
}
一切顺利,我可以用指针做所有的操作,但是,我其实很困惑。
编辑:
这就是我解决问题的方法,它有效,但看起来不对。我怎样才能创建一个构造函数来完成所有这些:
1) 我给 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) 然后,当我像这样声明一个矩阵数组时:
Matrix<Complex<int>> *niz = new Matrix<Complex<int>>[nr];
它创建一个大小为 1x1 的 nr 矩阵数组;
然后,当我打电话时:
niz[0].setNM(n,n);
它基本上破坏了旧矩阵并创建了一个新的一个大小的 nxn。我怎样才能通过这个电话完成所有这些工作:
Matrix<Complex<int>> *niz = new Matrix<Complex<int>>(n,n)[nr];
我不知道要做什么构造函数。
虽然我同意其他人的观点,即您遇到的错误解释了您的情况,但我不同意严厉的否决票。这里的实际问题似乎是您想使用非默认构造函数初始化对象数组。
不幸的是,没有直接的方法可以做到这一点。看看this answer.
你的例子
Matrix<Complex<int>> **niz=new Matrix<Complex<int>>*[nr];
是解决这个问题的一种方法。另一种是使用vector.