I am trying to make a Matrix class that has overloaded operators for +, =, and *. I am stuck on overloading + and =.
The problem here is that the compiler won't let me return 0. If the matrices are not the same size, I want the function (or whole program) to end there. How can I end the function?Code://assignment operator
Matrix &Matrix::operator=(Matrix &whatever) {
if(getRows() != whatever.getRows() || getCols() != whatever.getCols()) {
cout<<"The matrices are not the same size!"<<endl; //if they are the same size
return 0;
}
else { //else if they are not the same size
for(int r=0;r<getRows();r++)
for(int c=0;c<getCols();c++)
this->getTerms()[r][c] = whatever.getTerms()[r][c]; //set the term
} //end else
return *this;
} //END OPERATOR=
Here there is the same problem in the if statement as operator=. But also, I thought I should not return a reference a because 1) a new matrix needs to be made and 2) I would be returning a reference to a local variable. But whenever I try to use the operator, the program stops working. How can I make this work? Any help is appreciated.Code:Matrix Matrix::operator+(Matrix &additive) {
if( (getRows() != additive.getRows()) || (getCols() != additive.getCols()) ) //if they are not the same size
cout<<"The matrices cannot be added because they are not the same size!"<<endl;
else {
Matrix sum(getRows(), getCols(), 'S');
for(int r=0;r<getRows();r++)
for(int c=0;c<getCols();c++)
sum.getTerms()[r][c] = getTerms()[r][c] + additive.getTerms()[r][c];
return sum;
} //end else
} //END OPERATOR+