Witam,
piszę program wykonujący podstawowe operacje na macierzach. Docelowo ma to być program okienkowy, jednak same operacje na macierzach sprawdzam na konsoli. Pisząc przeciążenie dla operatora+ zauważyłem, że program wykrzacza się. Co ciekawe, gdy usunę destruktor dodawanie działa i podaje dobry wynik. Nie mam pojęcia co może być tutaj źle. Proszę o pomoc.
Zamieszczam obciętą wersję kodu:
macierz.h
#include <iostream>
class macierz {
private:
int w,k;
int** t;
public:
macierz(int n);
macierz(int n, int m);
~macierz();
macierz operator+ (const macierz& b);
macierz& operator= (const macierz b);
void set();
};
macierz.cpp
#include "macierz.h"
#include <iostream>
using namespace std;
macierz::macierz(int n) {
t = new int* [n];
for(int i=0; i<n; i++)
t[i] = new int [n];
w=n;
k=n;
}
macierz::macierz(int n, int m) {
t = new int* [n];
for(int i=0; i<n; i++)
t[i] = new int [m];
w=n;
k=m;
}
macierz::~macierz() {
for(int i=0; i<w; i++)
delete [] t[i];
delete [] t;
}
macierz& macierz::operator= (const macierz b) {
for(int i=0; i<w; i++)
for(int j=0; j<k; j++)
this->t[i][j] = b.t[i][j];
return *this;
}
macierz macierz::operator+ (const macierz& b) {
macierz c(w,k);
for(int i=0; i<w; i++)
for(int j=0; j<k; j++)
c.t[i][j] = this->t[i][j] + b.t[i][j];
return c;
}
void macierz::set() {
for(int i=0; i<w; i++)
for(int j=0; j<k; j++) {
cout << "t["<<i<<"] ["<<j<<"] = " ;
cin >> this->t[i][j];
}
}
main
#include "stdafx.h"
#include "macierz.h"
int _tmain(int argc, _TCHAR* argv[])
{
macierz a(2,2), b(2,2), c(2,2);
a.set();
b.set();
c = a+b;
return 0;
}