首先,这是代码,然后我将解释问题:
// 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类中添加了一个新方法:
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];
我不知道要构造什么构造函数。
答案 0 :(得分:0)
虽然我同意其他人的观点,但是你所得到的错误解释了你的情况,但我不同意严厉的贬低。这里的实际问题似乎是你想要使用非默认构造函数初始化对象和数组。
不幸的是,没有直接的方法可以做到这一点。看看this answer。
你的例子
Matrix<Complex<int>> **niz=new Matrix<Complex<int>>*[nr];
是克服这个问题的一种方法。另一种是使用vector。