0

My program keeps crashing and I can't see why...pretty sure its something to do with creating matrix B since when I remove the commands to create matrix B it runs ok. Can anyone see what is wrong?

Thanks in advance!

I have the header file:

```
// matrixlib.h - A header file for a simple matrix library
#include<string>
#include<vector>
class Matrix {
public:
Matrix(int mdim, int ndim);
// Create a zero matrix with mdim rows and ndim columns
Matrix(int ndim);
// Create an zero ndim x ndim matrix
void Add(Matrix b);
// Add Matrix b to this matrix
void SetElement(int m, int n, double d);
// Set Element at position (m,n) to the value d
private:
int mdim_; // Number of rows of matrix
int ndim_; // Number of columns of matrix
std::vector<double> data_; // Vector storing the matrix data
};
```

and main:

```
#include<iostream>
#include<vector>
#include<cmath>
#include "matrixlib.h"
using namespace std;
int main()
{
// Create a new instance of Matrix named A with 2 rows and 3 columns
Matrix A(2);
Matrix B(2);
A.SetElement(1,1,1);
A.SetElement(1,2,2);
A.SetElement(2,1,3);
A.SetElement(2,2,4);
B.SetElement(1,1,5);
B.SetElement(1,2,6);
B.SetElement(2,1,7);
B.SetElement(2,2,8);
A.Add(B);
return 0;
}
Matrix::Matrix(int mdim_, int ndim_)
{
data_.resize (mdim_*ndim_);
for (int i = 0; i < mdim_*ndim_; i++)
{
data_[i] = 0;
}
}
Matrix::Matrix(int ndim_)
{
data_.resize (ndim_*ndim_);
for (int i = 0; i < ndim_*ndim_; i++)
{
data_[i] = 0;
}
}
void Matrix::Add(Matrix b)
{
for (int i = 0; i < data_.size(); i++)
{
data_[i] += b.data_[i];
cout<<" "<<data_[i]<<" ";
}
}
// Add Matrix b to this matrix
void Matrix::SetElement(int m, int n, double d)
{
data_[((n*ndim_)-1)-(ndim_- m)] = d;
}
// Set Element at position (m,n) to the value d
```

*Edited 6 Years Ago by sexyzebra19*: n/a