Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Have a quick question regarding nonmember operation overloading in c++. My codes seems to be working for +, however when I'm trying to make a

Have a quick question regarding nonmember operation overloading in c++. My codes seems to be working for +, however when I'm trying to make a - as a nonmember, there are tons of compilation errors. Also, my * operator is giving me segmentation fault....

Here are my codes:

#ifndef MYMATRIX_H

#define MYMATRIX_H

#include

#include

#include

using namespace std;

/* I'm using this matrix to store data */

template

using twoD = std::vector>;

template

class Matrix{

private:

int rows;

int cols;

twoD matrix;

protected:

void validSizeCheck(int rows, int cols);

public:

Matrix(int rows, int cols);

Matrix(int rows, int cols, twoD newMatrix);

twoD getMatrix();

int getRows();

int getCols();

void printMatrix();

void operator =(const Matrix &);

Matrix &operator+=(const Matrix &);

Matrix operator+(const Matrix &);

Matrix operator*(const Matrix &);

Matrix operator*(const T &);

Matrix &operator*=(const Matrix &);

friend Matrix operator-(const Matrix &, const Matrix &);

};

/* checking for the valid size */

template

void Matrix::validSizeCheck(int rows, int cols){

if(rows < 1 || cols < 1){

cout<<"Wrong size!"<

throw exception();

}

}

/* matrix getter */

template

twoD Matrix::getMatrix(){

return matrix;

}

/* rows getter */

template

int Matrix::getRows(){

return rows;

}

/* column getter */

template

int Matrix::getCols(){

return cols;

}

/* default constructor */

template

Matrix::Matrix(int rows, int cols): rows(rows), cols(cols){

validSizeCheck(rows, cols);

matrix.resize(rows);

for(int i = 0; i < rows; i++){

matrix[i].resize(cols);

for(int j = 0; j < cols; j++){

matrix[i][j] = 0;

}

}

}

/* constructor */

template

Matrix::Matrix(int rows, int cols, twoD newMatrix): rows(rows), cols(cols){

validSizeCheck(rows, cols);

matrix.resize(rows);

for(int i = 0; i < rows; i++){

matrix[i].resize(cols);

for(int j = 0; j < cols; j++){

matrix[i][j] = newMatrix[i][j];

}

}

}

/* needed an = operator to supports c=a+b */

template

void Matrix::operator=(const Matrix &rhs){

if(rows != rhs.rows || cols != rhs.cols){

cout<<"Wrong size!"<

throw exception();

}

Matrix newMatrix(rows, cols);

for(int i = 0; i < rows; i++){

for(int j = 0; j < cols; j++){

newMatrix.matrix[i][j] = rhs.matrix[i][j];

}

}

}

/* overloading the operator+ */

template

Matrix Matrix::operator+(const Matrix &rhs){

if(rows != rhs.rows || cols != rhs.cols){

throw exception();

}

Matrix newMatrix(rows, cols);

for(int i = 0; i < rows; i++){

for(int j = 0; j < cols; j++){

newMatrix.matrix[i][j] = matrix[i][j]+rhs.matrix[i][j];

}

}

return newMatrix;

}

/* += operator will just use the operator+ */

template

Matrix &Matrix::operator+=(const Matrix &rhs){

*this = *this + rhs;

return *this;

}

/* *= operator will just use the operator* */

template

Matrix &Matrix::operator*=(const Matrix &rhs){

*this = *this * rhs;

return *this;

}

/* overloading operator* for 2 matrices*/

template

Matrix Matrix::operator*(const Matrix &rhs){

if(cols!=rhs.rows){

/* needs lhs |cols| to be same as rhs |rows| */

throw exception();

}

/* return matrix will be the size of the lhs rows and rhs cols */

Matrix tmp(rows, rhs.cols);

tmp.printMatrix();

for(int i = 0 ; i

for(int j = 0; i < rhs.cols; j++){

tmp.matrix[i][j] = 0; /* populates new matrix */

for(int k = 0; k

tmp.matrix[i][j] = tmp.matrix[i][j]+(matrix[i][k]*rhs.matrix[k][j]);

}

}

}

return tmp;

}

/* overloading operator* for constants */

template

Matrix Matrix::operator*(const T &constant){

Matrix temp(rows,cols);

for(int i = 0; i < rows; i++){

for(int j = 0; j < cols; j++){

temp.matrix[i][j] = matrix[i][j]*constant;

}

}

return temp;

}

/* friend operation overloading - */

template

Matrix Matrix::operator-(const Matrix& lhs,const Matrix& rhs){

if(lhs.rows != rhs.rows || lhs.cols != rhs.cols){

throw exception();

}

Matrix tmp(lhs.rows, rhs.cols);

for(int i = 0; i < lsh.rows; i++){

for(int j = 0; j < lsh.cols; j++){

tmp.matrix[i][j] = lsh.matrix[i][j]-rhs.matrix[i][j];

}

}

return tmp;

}

/* printMatrix */

template

void Matrix::printMatrix(){

for(int i = 0; i < rows; i++){

for(int j = 0; j < cols; j++){

cout<< matrix[i][j]<< " ";

}

cout<< endl;

}

cout<< "<<<--->>>"<< endl;

cout << endl;

}

#endif

Here is my main:

#include

#include "myMatrix.h"

using namespace std;

int main() {

cout<<"<<<<<-----Hello, myMatrix----->>>>>"<

twoD tmp1 = {{1,2},{3,4}};

Matrix m1(2,2,tmp1);

cout<<" m1:"<

m1.printMatrix();

twoD tmp2 = {{2,2},{4,5}};

Matrix m2(2,2,tmp2);

cout<<"m2:"<

m2.printMatrix();

Matrix m3 = m1+m2;

cout<<"m3 = m1+m2:"<

m3.printMatrix();

Matrix m4 = m1*2;

cout<<"m4 = m1*2"<

m4.printMatrix();

// Matrix m5 = m4-m2;

// cout<<"m5 = m4-m2"<

// m5.printMatrix();

cout<<"<<<<<-----Fairwell, myMatrix----->>>>>"<

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions