Well whenever I do that I get a ton (71 to be exact) of compiler errors. This is what I'm compiling for Matrix.h and Matrix.cpp.
Code:
#ifndef MATRIX_H
#define MATRIX_H
#pragma once
#include <iostream>
using std::ostream;
using std::istream;
Matrix &operator+=(Matrix &, const Matrix &); //overload +=
Matrix &operator+(Matrix &, const Matrix &); //overload +
class Matrix
{
friend ostream &operator<<(ostream &, const Matrix &); //printing matricies
public:
Matrix(const Matrix &);
Matrix(int, int, char);
~Matrix(void);
void setTerms(); //puts terms into the matrix
//getter functions
char getName() const;
int getRows() const;
int getCols() const;
double **getTerms() const;
//overloaded operators
Matrix &operator=(const Matrix &); //assignment operator
Matrix &operator*(double &); //multiply matrix by a non-matrix
Matrix &operator*(Matrix &); //multiple matrix by another matrix
//Matrix operator+(const Matrix &); //add one matrix to another. DNR REFERENCE
private:
double **terms;
int const rows;
int const cols;
char const name;
};
#endif
Code:
//MATRIX.CPP
#include "Matrix.h"
#include "DifferentSizeException.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
Matrix &operator+=(Matrix &additive1, const Matrix &additive2) {
if( (additive1.getRows() != additive2.getRows()) || (additive1.getCols() != additive2.getCols()) )
throw DifferentSizeException();
else {
for(int r=0;r<additive1.getRows();r++)
for(int c=0;c<additive1.getCols();c++)
additive1.getTerms()[r][c] += additive2.getTerms()[r][c];
} //end else
return additive1;
} //END OPERATOR+=
Matrix &operator+(Matrix &additive1, const Matrix &additive2) {
return additive1 += additive2;
} //END OPERATOR+
Matrix::Matrix(const Matrix &aMatrix)
: rows(aMatrix.getRows()), cols(aMatrix.getCols()), name(aMatrix.getName()) {
terms = new double *[rows];
for(int i=0;i<rows;i++)
*(terms+i) = new double[cols];
for(int r=0;r<getRows();r++)
for(int c=0;c<getCols();c++)
getTerms()[r][c] = aMatrix.getTerms()[r][c];
} //END COPY CONSTRUCTOR
//constructor
Matrix::Matrix(int r, int c, char n)
: rows(r), cols(c), name(n) {
terms = new double *[getRows()];
for(int i=0;i<getRows();i++)
*(terms+i) = new double[cols];
} //END CONSTRUCTOR W/ NAME
//destructor
Matrix::~Matrix(void)
{ delete [] terms; } //delete 2d array
//GETTER FUNCTIONS
char Matrix::getName() const {return name;}
int Matrix::getRows() const {return rows;}
int Matrix::getCols() const {return cols;}
double **Matrix::getTerms() const {return terms;}
//END GETTER FUNCTIONS
//user inputs terms to be put into the matrix
void Matrix::setTerms() {
cout<<"**********Matrix "<<getName()<<"**********"<<endl;
for(int r=0;r<rows;r++) {
for(int c=0;c<cols;c++) {
double theTerm;
cout<<"Enter the term for element ["<<r<<", "<<c<<"]."<<endl;
cin>>theTerm;
getTerms()[r][c] = theTerm;
} //end inner for
} //end double for
} //END SETTERMS()
//print matrix
ostream &operator<<(ostream &output, const Matrix &theMatrix) {
cout<<"--Matrix "<<theMatrix.getName()<<"--"<<endl;
for(int r = 0;r<theMatrix.getRows();r++) {
for(int c = 0;c<theMatrix.getCols();c++) {
output<<theMatrix.getTerms()[r][c]<<"\t";
if( (c+1) == theMatrix.getCols() )
output<<endl;
} //end inner for
} //end double for
return output;
} //END OPERATOR<<
//assignment operator
Matrix &Matrix::operator=(const Matrix &whatever) {
if(getRows() != whatever.getRows() || getCols() != whatever.getCols())
throw DifferentSizeException();
else { //else if they are not the same size
for(int r=0;r<getRows();r++)
for(int c=0;c<getCols();c++)
getTerms()[r][c] = whatever.getTerms()[r][c]; //set the individual term
} //end else
return *this;
} //END OPERATOR=
//multiply matrix by a number
Matrix &Matrix::operator*(double &constant) {
for(int r=0;r<getRows();r++)
for(int c=0;c<getCols();c++)
getTerms()[r][c] *= constant;
return *this;
} //END OPERATOR*(DOUBLE)
//Matrix Matrix::operator+(const Matrix &additive) {
// if( (getRows() != additive.getRows()) || (getCols() != additive.getCols()) ) //if they are not the same size
// throw DifferentSizeException(); //throw exception
// 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; //return the sum
// } //end else
//} //END OPERATOR+
//TODO*********************************
//multiply matrix by another matrix
Matrix &Matrix::operator*(Matrix &secondFactor) {
return *this;
} //END OPERATOR*(MATRIX)
I know most of that is unnecessary, but I'm including it just to show exactly what I'm compiling. I feel like I get the gist of what you're saying, but I'm slightly off. Declaring and implementing the operator functions where they are at is what I'm getting from what you said. What is wrong with it?