【发布时间】:2017-09-29 19:52:45
【问题描述】:
我有一个 Matrix (Matriz) 类实现如下:
标题:
#ifndef MATRIZ_H
#define MATRIZ_H
class Matriz
{
public:
Matriz(unsigned int nL, unsigned int nC);
~Matriz();
Matriz& operator+=(const Matriz &ptr);
const Matriz operator+(const Matriz &ptr) const;
Matriz* subtracaoMatriz(Matriz *m);
Matriz* multiplicacaoMatriz(Matriz *m);
void inserirMatriz();
void imprimirMatriz();
int verificaOperacao(const Matriz& ptr);
Matriz& operator-=(const Matriz &ptr);
const Matriz operator-(const Matriz &ptr) const;
const Matriz operator*(const Matriz &ptr) const;
protected:
private:
unsigned int nLinhas;
unsigned int nColunas;
int** matrix;
int verificaOperacao(Matriz *m); //0 -> cannot make the operation; 1 -> OK for product; 2 -> OK for sum;
};
#endif // MATRIZ_H
实施:
#include "Matriz.h"
#include <iostream>
using namespace std;
Matriz::Matriz(unsigned int nL, unsigned int nC)
{
this->nLinhas = nL;
this->nColunas = nC;
this->matrix = new int*[nLinhas];
for (unsigned int i = 0; i < nLinhas; ++i)
this->matrix[i] = new int[nColunas];
for(unsigned int i = 0; i < nLinhas; i++)
for(unsigned int j = 0; j < nColunas; j++)
this->matrix[i][j] = 0;
}
Matriz::~Matriz()
{
//dtor
}
int Matriz::verificaOperacao(Matriz *m)
{
if((this->nLinhas == m->nLinhas) && (this->nColunas == m->nColunas))
return 2;
else if(this->nColunas == m->nLinhas)
return 1;
else
return 0;
}
int Matriz::verificaOperacao(const Matriz& ptr)
{
if((this->nLinhas == ptr.nLinhas) && (this->nColunas == ptr.nColunas))
return 2;
else if(this->nColunas == ptr.nLinhas)
return 1;
else
return 0;
}
Matriz& Matriz::operator+=(const Matriz &ptr) {
if(this->verificaOperacao(ptr) == 2)
{
for(unsigned int i = 0; i < this->nLinhas; i++)
for(unsigned int j = 0; j < this->nColunas; j++)
this->matrix[i][j] = this->matrix[i][j] + ptr.matrix[i][j];
return *this;
}
else
return *this;
}
const Matriz Matriz::operator+(const Matriz &ptr) const {
Matriz resultado = *this;
resultado += ptr;
return resultado;
}
Matriz& Matriz::operator-=(const Matriz &ptr) {
if(this->verificaOperacao(ptr) == 2)
{
for(unsigned int i = 0; i < this->nLinhas; i++)
for(unsigned int j = 0; j < this->nColunas; j++)
this->matrix[i][j] = this->matrix[i][j] - ptr.matrix[i][j];
return *this;
}
else
return *this;
}
const Matriz Matriz::operator-(const Matriz &ptr) const {
Matriz resultado = *this;
resultado -= ptr;
return resultado;
}
const Matriz Matriz::operator*(const Matriz &ptr) const {
Matriz *resultado = new Matriz(this->nLinhas, ptr.nColunas);
for(unsigned i = 0; i < this->nLinhas; i++)
{
for(unsigned j = 0; j < ptr.nColunas; j++)
for(unsigned int aux = 0; aux < ptr.nColunas; aux++)
resultado->matrix[i][j] += this->matrix[i][aux] * ptr.matrix[aux][j];
}
return *resultado;
}
void Matriz::inserirMatriz()
{
for(unsigned int i = 0; i < this->nLinhas; i++)
for(unsigned int j = 0; j < this->nColunas; j++)
cin >> this->matrix[i][j];
}
void Matriz::imprimirMatriz()
{
for(unsigned int i = 0; i < this->nLinhas; i++) {
for(unsigned int j = 0; j < this->nColunas; j++)
cout << this->matrix[i][j] << "\t";
cout << endl;
}
}
主要:
#include <iostream>
#include "Matriz.h"
using namespace std;
int main()
{
Matriz *m1 = new Matriz(2, 2);
Matriz *m2 = new Matriz(2, 2);
m1->inserirMatriz();
m2->inserirMatriz();
cout << "Matrix 1:" << endl;
m1->imprimirMatriz();
cout << "Matrix 2:" << endl;
m2->imprimirMatriz();
Matriz m3 = *m1 + *m2;
cout << "The sum is: " << endl;
m3.imprimirMatriz();
cout << "The subtraction is: " << endl;
Matriz m4 = *m1 - *m2;
m4.imprimirMatriz();
cout << "The product is: " << endl;
Matriz m5 = *m1 * *m2;
m5.imprimirMatriz();
///HERE LIES THE PROBLEM
m2 = m1;
cout << "m2 = m1" << endl;
cout << "m2:" << endl;
m2->imprimirMatriz();
cout << "*m1 += *m2" << endl;
cout << "m2:" << endl;
*m1 += *m2;
m2->imprimirMatriz();
delete m1;
delete m2;
return 0;
}
我相信这个问题是由默认的复制构造函数引起的,但是我尝试实现一个但没有成功。
【问题讨论】:
-
什么是“问题”?问题是什么?
-
如果你问我,问题是你没有明显的原因进行手动动态内存分配。为什么是
m1和m2指针?为什么不使用std::vector? -
您说您认为问题来自默认的复制构造函数,但您没有说明问题是什么?
-
您更改了标题,但我不明白代码的相关性。为动态矩阵实现复制构造函数的正确方法是:你没有,其他人已经为你做了,只需使用
std::vector -
顺便说一句,您不需要
this->语法。仅当参数名称与数据成员名称相同时才需要它。不使用this->将节省您的打字时间。 :-)
标签: c++ pointers copy-constructor