Program obsługujący macierze w klasach. POMOCY!

0

Witam was. Napisalem program, ktory obsluguje wyjatki, oraz operuje na macierzach, i jego funkcje zwracaja nowe macierze. Ale cos jest nie tak.
Podczas testowania program wywala blad i zatrzymuje sie.

Blad jest taki :

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

 




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

class array
{
public:
	array(int a, int b)
{
	arr=new int *[a];
	for(int i=0;i<a;i++)
	arr[i]= new int[b];
	x=a;
	y=b;
};
	~array()
	{
		for(int i=0;i<x; i++)
			delete[] arr[i];
				delete[] arr;
	};
	void show();
	void set();
	void change(int, int, int);
	array addNumb(int);
	void transpose();
	void load(string);
	void save(string);
	array add(array a) throw (string) ;
	array sub(array a) throw (string);
	array mno(array a) throw (string);
int x,y;
int **arr;
};

int _tmain(int argc, _TCHAR* argv[])
{
	
	array one(2,2);
	one.set();
	array three(5,5);
	three=one.addNumb(5);
	
	getchar();
	return 0;
}

void array::transpose()
{
	int** temp;
	temp=new int *[y];
	for(int i=0;i<y;i++)
	temp[i]= new int[x];

     for(int i = 0; i <x; ++i)
          for(int j = 0; j < y; ++j)
               swap(arr[i][j], temp[j][i]);
	 int f;
	 f=x;
	 x=y;
	 y=f;
	 arr=temp;
}

void array::change(int a, int b, int n)
{
	if(a<x || b<y)
		arr[a][b]=n;
	else
		cout<< "\n Error, wrong dimensions. \n";
};

array array::addNumb(int n) 
{
	array temp(x,y);

	for(int i=0;i<x;i++)
		for(int j=0;j<y;j++)
			temp.arr[i][j]=arr[i][j]+n;

	return temp;
};

void array::set()
{
	for(int i=0; i<x; i++)
		for(int j=0; j<y;j++)
			arr[i][j]=0;
}

void array::show()
{
	for(int i=0; i<x; i++)
		{
			for(int j=0; j<y;j++)
			cout << arr[i][j] << " ";
			cout << "\n";
		}	
	}
void array::save(string a)
{
	ofstream file(a, ios::in | ios::out | ios::app);
		file << x << " " << y << "\n";
	
		for(int i=0; i<x; i++)
		{
			for(int j=0; j<y; j++)
			{
			file << arr[i][j] << " ";
			}
			file<<"\n";
		}
	file.close();
};

void array::load(string a)
{
ifstream file(a);
file >> x >> y;
for (int i = 0; i < x; i++) 
{
    for (int j = 0; j < y; j++)
	{
        file >> arr[i][j];
    }
}
};

array array::add(array a) throw (string)
{
	array temp (x,y);
	if((a.x)!=x || (a.y)!=y)
	{
		string err = " Can't add, wrong dimensions. \n"; throw err;
	}
		
	else
	{
		
	for(int i=0;i<x;i++)
		for(int j=0;j<y;j++)
			temp.arr[i][j]=(a.arr[i][j])+(arr[i][j]);
	}
	return temp;
};

array array::sub(array a) throw (string)
{
	array b(x, y);
	if((a.x)!=x || (a.y)!=y)
	{
		string err = " Can't add, wrong dimensions. \n"; throw err;
	}
	else
	{
	
	for(int i=0;i<x;i++)
	{
		for(int j=0;j<y;j++)
			b.arr[i][j]=((arr[i][j])-(a.arr[i][j]));
	}
	}
	return b;
};
array array::mno(array a) throw (string)
{
	array temp(x,y);
	temp.arr=this->arr;
	if((a.y)!=x)
	{
		string err = " Can't mno, wrong dimensions. \n"; throw err;
	}
	else 
	{
	for(int i=0; i<a.x; i++)
 {
   for(int j=0; j<y; j++)
     {
       temp.arr[i][j]=0;
         for(int k=0; k<a.y; k++)
          {
           temp.arr[i][j] += ((a.arr[i][k]) * (temp.arr[k][j]));
         }
     }
 } 
	}
	return temp;
};
0

Gdzieś błędnie odwołujesz się do pamięci. Generalnie cały ten kod jest słaby, więc ciężko powiedzieć.
Na oko tutaj:

    temp=new int *[y];
    for(int i=0;i<y;i++)
    temp[i]= new int[x];
 
     for(int i = 0; i <x; ++i)
          for(int j = 0; j < y; ++j)
               swap(arr[i][j], temp[j][i]);

Powinno być arr[j][i].

0

W tym miejscu np to nie jest istotne, ponieważ nie wykorzystuje tej funkcji do mnozenia, dodawania, odejmowania..

1

Przeleć kod debuggerem patrząc na wszystkie adresy.

1

Brakuje konstruktora kopiującego i operatora przypisania. Poczytaj o The rule of three.
Btw, specyfikator throw jest deprecated i on nigdy nie miał sensu.

0

A do czego mialby posluzyć tutaj konstruktor kopiujacy?
niestety dalej nie moge sie dobrac do tego programu.
Ale już biorę się, do poczytania, o czym napisałeś wyżej!

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