I am trying to create a Matrix class using c++. So far, I have the following accomplished:
- Creating the Matrix
- Deleting the Matrix
- Getting and setting values inside of the matrix
Right now, I am working on overriding all of the operators (ie., +, -, *, /) and returning matrices. I'm having a lot of issues on this, so I was wondering if anyone could help?
And I'm also having issues with copying the matrix into a new one, so any help with that code would be appreciated.
Note: I am coming from a Python background, and I know a bit of c++. I decided that while yes, creating a lot of really cool games and OOP stuff in Python is awesome and cool, I should learn c++ in order to be getting a job when I'm older.
Here's my code, I have a header containing the prototype and the class definitions, then the main one.
matrix.h
#ifndef MATRIX_H
#define MATRIX_H
/*
// These are all of the error codes
// Upon changing errorcode, Matrix should reset to null/void
*/
#define ERROR_ROW_NP 1 // Row Number cannot be non-positive
#define ERROR_COLUMN_NP 2 // Column Number cannot be non-positive
#define ERROR_ROW_I 3 // Row Index Error
#define ERROR_COLUMN_I 4 // Column Index Error
#define ERROR_RC_MISMATCH 5 // # of Rows and Columns do not match
class Matrix {
int row;
int column;
int elements;
int *RC;
public:
int ERRORCODE;
Matrix (void); // DONE
Matrix (int, int); // DONE
~Matrix (void); // DONE
void Copy (Matrix);
int get_value (int, int); // DONE
void set_value (int, int, int); // DONE
int rc_match (Matrix); // DONE
Matrix operator+ (Matrix);
Matrix operator- (Matrix);
Matrix operator* (Matrix);
Matrix operator* (int);
Matrix operator/ (int);
};
#endif
matrix.cpp
#include "matrix.h"
Matrix::Matrix (void) {
ERRORCODE = 0;
row = 1;
column = 1;
elements = row * column;
RC = new int[elements];
for (int i=0; i< elements; i++) {
RC[i] = 0;
}
}
Matrix::Matrix (int r, int c) {
ERRORCODE = 0;
row = r;
column = c;
elements = row * column;
RC = new int[elements];
for (int i=0; i< elements; i++) {
RC[i] = 0;
}
}
Matrix::~Matrix (void) {
delete[] RC;
}
// Copy will copy all of the contents of the toCopy
// matrix into itself; also resets it's own rows/columns
void Matrix::Copy (Matrix toCopy) {
row = toCopy.row;
column = toCopy.column;
elements = toCopy.elements;
RC = new int[elements];
for (int i=0; i<elements; i++) {
RC[i] = toCopy.RC[i];
}
}
int Matrix::get_value (int r, int c) {
return RC[(column*r)+c];
}
void Matrix::set_value (int r, int c, int value) {
RC[(column*r)+c] = value;
}
int Matrix::rc_match (Matrix a) {
if (
(row == a.row)
&&
(column == a.column)
) {
return (1);
}
else {
return (0);
}
}
Matrix Matrix::operator+ (Matrix a) {
if (rc_match(a)) {
Matrix OUT(row, column);
int z;
for (int i=0; i < row; i++) {
for (int j=0; j < column; j++) {
z = OUT.get_value(i, j) + a.get_value(i, j);
OUT.set_value(i, j, z);
}
}
return OUT;
}
else {
Matrix OUT(1, 1);
OUT.ERRORCODE = ERROR_RC_MISMATCH;
return OUT;
}
}
main.cpp
#include <iostream>
#include "matrix.h"
int main(void) {
Matrix a(2, 2);
a.set_value(0, 0, 3);
a.set_value(0, 1, 2);
Matrix b(2, 2);
b.set_value(0, 0, 1);
b.set_value(0, 1, 1);
b.set_value(1, 0, 3);
b.set_value(1, 1, 3);
printf("%d %d\n", a.get_value(0, 0), a.get_value(0, 1));
printf("%d %d\n", a.get_value(1, 0), a.get_value(1, 1));
printf("\n");
printf("%d %d\n", b.get_value(0, 0), b.get_value(0, 1));
printf("%d %d\n", b.get_value(1, 0), b.get_value(1, 1));
char t[1];
printf("Press 'Enter' to continue...");
std::cin.getline(t, 1);
printf("\n");
Matrix c;
c.Copy(a+b);
printf("%d %d\n", c.get_value(0, 0), c.get_value(0, 1));
printf("%d %d\n", c.get_value(1, 0), c.get_value(1, 1));
printf("Press 'Enter' to continue...");
std::cin.getline(t, 1);
printf("\n");
return (0);
}
The error I am getting upon compiling and running is this:
Debug assertion failed! ...
Expression: _BLOCK_TYPE_IS_VALID(pHead ->nBlockUse)
That pops up after hitting 'Enter'
Also, this is my first time posting, if I did anything wrong, let me know please :]
EDIT2: I got it to work! Thank you @templatetypedef!
Here's the additional code I used:
(I found out that my add function was wrong too)
matrix.cpp
Matrix::Matrix(const Matrix& toCopy) {
row = toCopy.row;
column = toCopy.column;
elements = toCopy.elements;
RC = new int[elements];
for (int i=0; i<elements; i++) {
RC[i] = toCopy.RC[i];
}
}
Matrix Matrix::operator+ (Matrix a) {
if (rc_match(a)) {
Matrix OUT(row, column);
int z;
for (int i=0; i < row; i++) {
for (int j=0; j < column; j++) {
z = get_value(i, j) + a.get_value(i, j);
OUT.set_value(i, j, z);
}
}
return OUT;
}
else {
Matrix OUT(1, 1);
OUT.ERRORCODE = ERROR_RC_MISMATCH;
return OUT;
}
}
So for now I shall look into the assignment operator