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

Recommended Answers

All 4 Replies

The main problem with that class is that the constructor with only one constructor does not initialize the value of both class member variables mdim_ and ndim_.

Thanks for the reply Ancient Dragon, I am not able to change anything in the header file, so is there a way to initalize these variables within the .cpp file?

I tried setting them to 0 within the Matrix:: functions but no luck...

I am not able to change anything in the header file, so is there a way to initalize these variables within the .cpp file?

Yes, there is. For example,

Matrix::Matrix(int mdim_, int ndim_)
{
  this->mdim_ = mdim_;
  this->ndim_ = ndim_;

You could/should also change to ..

Matrix::Matrix(int [I]rows[/I], int [I]cols[/I])
{
  mdim_ = rows;
  ndim_ = cols;

That would be more clear, as it is quite confusing when the incoming arguments are named exactly as the member variables.

And furthermore, given a matrix class, sticking to variable names in terms of rows/columns would be more readable (i.e. instead of mdim_/ndim_ and also plain m and n elsewhere in the code).

Note that you have to fix both constructors so that the dimensions are stored properly upon construction.

Fantastically helpful as usual, its working fine now thanks mitrmkar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.