所以我想在我的类矩阵中重载operator +(C ++)来使用这个指令 m1 = m2 + m3
知道我已经超载了操作符=但它在执行时崩溃了 这是我的代码 类矩阵:
#include "../include/matrices.h"
#include <iostream>
using namespace std;
class matrices
{
public:
int taille;
int **tab;
public:
matrices();
matrices(int);
matrices(matrices &);
virtual ~matrices();
void setTaille(int);
int getTaille();
void setVal(int, int, int);
int getVal(int, int);
friend istream& operator >> (istream&, matrices& ); //surcharge de l'opérateur >>
friend ostream& operator << (ostream&, const matrices& ); //surcharge de l'opérateur <<
//void operator=(matrices&);
matrices& operator=(matrices& );
//friend matrices& operator+(matrices , matrices );
friend matrices& operator+(matrices&, matrices&);
friend matrices operator-(matrices);
friend matrices operator*(matrices);
};
matrices::~matrices()
{
//dtor
}
matrices::matrices()
{
taille = 2;
this->tab = new int*[taille];
for(int i=0; i<taille; i++)
this ->tab[i] = new int[taille];
}
matrices::matrices(int n)
{
taille = n;
this->tab = new int*[taille];
for(int i=0; i<taille; i++)
this->tab[i] = new int[taille];
}
matrices::matrices(matrices & m1)
{
(*this).taille = m1.taille;
tab = new int*[(*this).taille];
for(int i=0; i<(*this).taille; i++)
for(int j=0; j<(*this).taille; j++)
this->tab[i][j] = m1.tab[i][j];
}
void matrices::setTaille(int n)
{
(*this).taille = n;
}
int matrices::getTaille()
{
return (*this).taille;
}
void matrices::setVal(int val, int n, int m)
{
this->tab[n][m] = val;
}
int matrices::getVal(int n, int m)
{
return this->tab[n][m];
}
istream& operator >> (istream& flux, matrices& m1)
{
for(int i=0; i<m1.taille; i++)
{
for(int j=0; j<m1.taille; j++)
{
flux >> m1.tab[i][j];
}
}
return (flux);
}
ostream& operator << (ostream& flux, const matrices& m1)
{
for(int i=0; i<m1.taille; i++)
{
for(int j=0; j<m1.taille; j++)
{
flux << m1.tab[i][j];
cout << "\t" ;
}
cout << "\n" << endl;
}
return (flux);
}
matrices& matrices::operator=(matrices& m1)
{
delete this->tab;
(*this).taille = m1.taille;
this->tab = new int*[m1.taille];
for(int i=0; i<m1.taille; i++)
this->tab[i] = new int[m1.taille];
for(int i=0; i<m1.taille; i++)
{
for(int j=0; j<m1.taille; j++)
{
this->tab[i][j] = m1.tab[i][j];
}
}
return (*this);
}
matrices& operator+(matrices& m1, matrices& m2)
{
matrices mtmp;
mtmp.taille = m1.taille;
mtmp.tab = new int*[mtmp.taille];
for(int i=0; i<mtmp.taille; i++)
mtmp.tab[i] = new int[mtmp.taille];
if(m2.taille == m1.taille)
{
for(int i=0; i<mtmp.taille; i++)
{
for(int j=0; j<mtmp.taille; j++)
{
mtmp.tab[i][j] = m1.tab[i][j] + m2.tab[i][j];
}
}
}
return mtmp;
}
这是我的主要功能
#include <iostream>
#include "include/matrices.h"
#include<stdlib.h>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
matrices m1,m2,m3;
cin >> m1;
cin >> m3;
m2 = m1;
cout << "=================================" << endl;
cout << "==== Affichage de la matrice ====" << endl;
cout << "=================================" << endl;
cout << m2;
getchar();
cout << "==================================" << endl;
cout << "==== sommation des 2 matrices ====" << endl;
cout << "==================================" << endl;
m3 = m1 + m2;
cout << "==================================" << endl;
cout << "==== matrice m3 matrices est: ====" << endl;
cout << "==================================" << endl;
cout << m3;
}
感谢您的帮助
答案 0 :(得分:3)
您必须要么使用不会对此发出警告的编译器,要么忽略警告:
matrices& operator+(matrices& m1, matrices& m2)
{
matrices mtmp;
...
return mtmp;
}
代码是个大问题:您正在尝试返回对自动变量的引用。当您尝试使用该引用时,该对象不再存在。
你应该用这种方式声明你的过载:
matrices operator+(const matrices& m1, const matrices& m2)
{
matrices mtmp;
...
return mtmp;
}
还需要您更改朋友声明:
friend matrices operator+(const matrices&, const matrices&);
这样你就会返回一个副本(因为你没有更改m1
和m2
,你应该将它们的引用声明为const
)。< / p>
此外,您应该查看复制赋值运算符的复制交换习惯用法,因为执行m1 = m1
之类的操作会导致当前代码崩溃。作为这一点的推论:你没有声明一个拷贝构造函数,所以当你做matricies m2 = m1
之类的事情时,它正在做一个浅拷贝(拷贝构造函数的默认实现)。所以m2
的指针指向m1
中的数据。当你破坏m1
时,m2
包含悬空指针......这将导致堆损坏(删除已经删除的内存)。
很多内存问题(并且很多)只需切换到std::vector
或std::array
(取决于您希望如何定义类)而不是尝试管理内存你自己。一个简单(不完整)的例子:http://ideone.com/VVqvGy
答案 1 :(得分:2)
你班上有很多问题。
new[]
delete
分配的内存(注意缺少[]):这是未定义的行为,并且是随机内容。您还应该删除所有动态分配的内存,而不仅仅是“顶层” - 与在构造函数的循环中分配它的方式相同,您应该在循环中释放它,并使用delete[]
。setTaille
方法毫无意义:它只会破坏您的数据,导致内存泄漏和数据不一致。它应该完全重做,重新分配和复制当前矩阵的内容以允许设置或完全删除大小。 (老实说,我不能想象它的用例)。operator+
中,您应该在执行任何分配之前检查矩阵的大小是否相同。您应该决定添加不兼容矩阵的结果应该是什么(这是一个艰难的选择)。 代码中的错误很可能来自错误编号2.
编辑:
我发现了另一个我忽略的问题:
在matrices& operator+
函数中,返回对自动变量的引用(即堆栈中的内存)。这是非法的:你离开这个功能的那一刻,这个记忆就变成了垃圾。您最有可能想要返回构造矩阵的副本(使用matrices operator+