Created
December 6, 2013 13:28
-
-
Save anonymous/7823794 to your computer and use it in GitHub Desktop.
I created a class called Matrix with a 2d array. I can run it with the constructor, copy constructor and destructor. When I introduce the unary negation operator, I get a runtime error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include"Matrix.h" | |
using namespace std; | |
void main() | |
{ | |
Matrix m(4, 5); | |
Matrix m2(m); | |
m2.setValue(1,2,12); | |
int x; | |
m2.getValue(1,2,x); | |
Matrix m3; | |
m3 = -m2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MATRIX_H | |
#define MATRIX_H | |
class Matrix | |
{ | |
public: | |
Matrix(int rSize=3, int cSize=3); | |
Matrix(const Matrix& m); | |
~Matrix(); | |
bool setValue(int rSize, int cSize, int value); | |
bool getValue(int rVal, int cVal, int& value)const; | |
const Matrix& operator- (); | |
private: | |
int rowSize; | |
int columnSize; | |
int** arr; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include"Matrix.h" | |
using namespace std; | |
Matrix::Matrix(int rSize,int cSize) | |
{ | |
columnSize = cSize; | |
rowSize = rSize; | |
arr = new int* [rowSize]; | |
for(int i=0; i<rowSize; i++) | |
arr[i] = new int[columnSize]; | |
for(int j=0; j<rowSize; j++) | |
{ | |
for(int k=0; k<columnSize; k++) | |
arr[j][k] = 0; | |
} | |
} | |
Matrix::Matrix(const Matrix& m) | |
{ | |
columnSize = m.columnSize; | |
rowSize = m.rowSize; | |
arr = new int* [rowSize]; | |
for(int i=0; i<rowSize; i++) | |
{ | |
arr[i] = new int [columnSize]; | |
} | |
for(int i=0; i<rowSize; i++) | |
{ | |
for(int j=0; j<columnSize; j++) | |
arr[i][j] = m.arr[i][j]; | |
} | |
} | |
Matrix::~Matrix() | |
{ | |
for(int i = 0; i < rowSize; ++i) | |
delete [] arr[i]; | |
delete [] arr; | |
} | |
bool Matrix::setValue(int rVal, int cVal, int value) | |
{ | |
if((rVal<0)||(cVal<0)||(rVal>rowSize-1)||(cVal>columnSize-1)) | |
return false; | |
arr[rVal][cVal] = value; | |
return true; | |
} | |
bool Matrix::getValue(int rVal, int cVal, int& value)const | |
{ | |
if((rVal<0)||(cVal<0)||(rVal>rowSize-1)||(cVal>columnSize-1)) | |
return false; | |
value = arr[rVal][cVal]; | |
return true; | |
} | |
const Matrix& Matrix:: operator- () | |
{ | |
Matrix m(*this); | |
for (int i = 0; i< rowSize; i++) | |
{ | |
for(int j=0; j<columnSize; j++) | |
m.arr[i][j] = (this->arr[i][j])* -1 ; | |
} | |
return m; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment