Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ sparse matrix class

Status
Not open for further replies.

lordleviathon

Programmer
Nov 8, 2002
1
0
0
US
I am having trouble with a matrix class I am writing. It will compile ok, but crashes as soon as I run the program.
On debugging it, I found that the problem occurs in the constructor. It happens when the constructor tries to assign the first value. If anyone could help me I would be grateful.

I have include the relevant code.



#include <iostream.h>

#ifndef MATRIX_OBJ_H
#define MAXTRIX_OBJ_H

typedef unsigned int index_t;
typedef double el_t;

typedef struct matrix_node* pointer_t;

struct matrix_node {
index_t row;
index_t col;
el_t value;
pointer_t right;
pointer_t down;

};


class Matrix {
pointer_t header;
int nonZeroCount;
pointer_t currR;
index_t row, col;
void MakeNode(pointer_t& newNode,index_t i,index_t j, el_t value);
void MakeNode(pointer_t& newNode,index_t i, index_t j);


public:
Matrix(index_t numRows, index_t numColumns);
Matrix(const Matrix& m);
~Matrix();

int numRows() {
return header->row;
}

int numColumns() {
return header->col;
}

int numNonZeros() {
return nonZeroCount;
}

bool isZero() {
return (nonZeroCount == 0);
}

bool rowIsZero(index_t i);
bool columnIsZero(index_t j);

el_t valueAt(index_t i, index_t j);
void setValueAt(index_t i, index_t j, el_t newValue);
Matrix transpose();
Matrix& operator=(const Matrix&);
Matrix operator*(const Matrix&);
Matrix operator*(double);
Matrix operator+(const Matrix&);
Matrix operator-(const Matrix&);
Matrix operator+=(const Matrix&);
Matrix operator-=(const Matrix&);
Matrix operator*=(double);
friend Matrix operator*(double, const Matrix&);

void toFirst();
bool atEndR();
bool atEndC();
void displayMatrix();
void fillMatrix();

};

/*****************************************************/
// Constructors //
/*****************************************************/


Matrix::Matrix(index_t numRows, index_t numColumns) {

header->row = numRows;
header->col = numColumns;
nonZeroCount = 0;
}
 
header->row = numRows;
header->col = numColumns;

....
I believe you have not initialized correctly the pointer header

Ion Filipski
1c.bmp
 
As Ion said you should initialize the header member (which is a pointer to a struct)e.g allocate memory from the heap.
In this case you MUST implement the Matrix destructor, revise the code for the assignment operator and also implement the copy constructor : Matrix(Matrix &ref);
otherwise other problems will appear.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top