I'm trying to build my skills here.
This code compiles and runs successfully if I go out of class.
It crashes when I need to enter numbers for matrices.
Can somebody explain to me why this crashes?
#pragma once
#ifndef matrixType_h
#define matrixType_h
template <class T>
class matrixType {
public:
matrixType() { rawMatrix1 = rawMatrix2 = addMatrices = subtractMatrices = multiplyMatrices = 0; };
void initialize(T row1, T col1);
void add();
void subtract();
void multiply();
private:
T ** rawMatrix1, **rawMatrix2, ** addMatrices, ** subtractMatrices, ** multiplyMatrices;
};
template <class T>
void matrixType<T>::initialize(T row1, T col1) {
for (int a = 0; a < row1; a++)
{
cout << "enter " << col1 << " numbers for row " << a + 1 << ":" << endl;
for (int b = 0; b < col1; b++)
cin >> rawMatrix1[a][b];
cout << endl;
}
}
template <class T>
void matrixType<T>::add()
{
cout << "adding" << endl;
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
addMatrices[a][b] = rawMatrix1[a][b] + rawMatrix2[a][b];
}
}
template <class T>
void matrixType<T>::subtract() {
cout << "subtracting" << endl;
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
subtractMatrices[a][b] = matrix1[a][b] - matrix2[a][b];
}
}
}
template <class T>
void matrixType<T>::multiply() {
cout << "multiplying" << endl;
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
for (int inner = 0; inner < 3; inner++)
{
multiplyMatrices[a][b] = matrix1[a][inner] * matrix2[inner][b];
}
}
}
}
#endif // !matrixType_h
#include <iostream>
#include "matrixType.h"
using namespace std;
int main() {
matrixType<int>matrix1;
int rows1, columns1, rows2, columns2;
cout << "Enter number of rows for matrix 1" << endl;
cin >> rows1;
cout << "Enter number of columns matrix 1" << endl;
cin >> columns1;
cout << "Enter number of rows for matrix 2" << endl;
cin >> rows2;
cout << "Enter number of columns matrix 2" << endl;
cin >> columns2;
matrix1.initialize(rows1, columns1);
return 0;
}