Wątek przeniesiony 2017-11-01 17:45 z C/C++ przez kq.

Problem z destruktorem.

0

Wygląda na to, że destruktor czyści mi tablice przed wywołaniem się funkcji mutiply i w zasadzie kompletnie nie mam pojęcia w czym może być problem, ponieważ jak tylko za komentuję destruktor wszystko działa poprawnie. Przy debugowaniu błąd wyskakuje w metodzie toString ale raczej nie w tym problem ponieważ tą konwersję robiłem na dwa sposoby.

Header:

#include <iostream>
#include "stdafx.h"

using namespace std;
class Matrix
{
public:

	double **tab;
	int w, k;
	
	Matrix(int, int); // w-wiersze k-kolumny
	Matrix mutiply(Matrix &, Matrix &);
	string toString(Matrix );
	
	~Matrix();
};

Destruktor:

 Matrix::~Matrix()
 {
	 for (int i = 0; i < w; i++)
	 {
		 delete[] tab[i];
	 }

	 delete[] tab;
	 cout << "Zwolnilem pamiec tablicy" << endl;
 }

Metoda mutiply:

 Matrix Matrix::mutiply(Matrix &aa, Matrix &bb)
{
	 Matrix cc(aa.w,bb.k);

	 double s = 0;
	 for (int i = 0; i < aa.w; i++)
		 for (int j = 0; j <bb.k; j++)
		 {
			 s = 0;
			 for (int k = 0; k < aa.k; k++) s += aa.tab[i][k] * bb.tab[k][j];
			 cc.tab[i][j] = s;
		 }
	return  cc;
}

Metoda toString:

 string Matrix::toString(Matrix aa)
 {
	 int length=0;
	 string temp,word;
	

	 for (int i = 0; i < aa.w; i++)
	 {
		 for (int j = 0; j < aa.k; j++)
		 {
			 temp = to_string(aa.tab[i][j]); // tutaj kompilator wyrzuca błąd (prawdopodobnie nie w tym problem)
			 if (temp.length() > length) length = temp.length();
		 }
	 }

	 for (int i = 0; i < aa.w; i++)
	 {
		 for (int j = 0; j < aa.k; j++)
		 {
			 ostringstream os;
			 os << setw(length) << aa.tab[i][j];
			 word += os.str() + " ";
		 }
		 word += "\n";
	 }

	return word;
 }

main:

#include "stdafx.h"
#include <iostream>
#include "Matrix.h"
#include <string>
using namespace std;

int main()
{
	Matrix a(4,5);
	Matrix b(5,6);

	for (int i = 0; i < a.w; i++)
	{
		for (int j = 0; j < a.k; j++)
		{
			a.tab[i][j] = i+0.03;
		}
	}

	for (int i = 0; i < b.w; i++)
	{
		for (int j = 0; j < b.k; j++)
		{
			b.tab[i][j] = i;
		}
	}

	if (a.k != b.w)
	{
		cout<<"Nieprawidlowy rozmiar Macierzy!";
		exit (0);
	}

	Matrix c = a.mutiply(a, b);
	string wynik;
	wynik = c.toString(c);
	cout << wynik;
	

    return 0;
}


1 użytkowników online, w tym zalogowanych: 0, gości: 1