Question
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
protected:
void validSizeCheck(int rows, int cols);
public:
Matrix(int rows, int cols);
Matrix(int rows, int cols, twoD
twoD
int getRows();
int getCols();
void printMatrix();
void operator =(const Matrix
Matrix
Matrix
Matrix
Matrix
Matrix
friend Matrix
};
/* checking for the valid size */
template
void Matrix
if(rows < 1 || cols < 1){
cout<<"Wrong size!"< throw exception(); } } /* matrix getter */ template twoD return matrix; } /* rows getter */ template int Matrix return rows; } /* column getter */ template int Matrix return cols; } /* default constructor */ template Matrix 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 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 if(rows != rhs.rows || cols != rhs.cols){ cout<<"Wrong size!"< throw exception(); } Matrix 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 if(rows != rhs.rows || cols != rhs.cols){ throw exception(); } Matrix 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 *this = *this + rhs; return *this; } /* *= operator will just use the operator* */ template Matrix *this = *this * rhs; return *this; } /* overloading operator* for 2 matrices*/ template Matrix 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.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 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 if(lhs.rows != rhs.rows || lhs.cols != rhs.cols){ throw exception(); } Matrix 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 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 Matrix cout<<" m1:"< m1.printMatrix(); twoD Matrix cout<<"m2:"< m2.printMatrix(); Matrix cout<<"m3 = m1+m2:"< m3.printMatrix(); Matrix cout<<"m4 = m1*2"< m4.printMatrix(); // Matrix // cout<<"m5 = m4-m2"< // m5.printMatrix(); cout<<"<<<<<-----Fairwell, myMatrix----->>>>>"< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started