,所以我想在我的類別矩陣超負荷運營商+(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;
}
,這是我的主要fonction
#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;
}
感謝您的幫助
不要你得到一個警告,關於參考返回到本地變量? –
野應存在只是執行 – mtgenius
期間crach你用調試器以確定現金在哪裏發生?如果是這樣,哪條線路導致崩潰?如果不是,爲什麼不? – Eric