// using an external function, you'll need to create a new
// matrix object to return. you don't want to return a
// reference to it.
//matrix& operator+ (const matrix& A, const matrix& B)
matrix operator+ (const matrix& A, const matrix& B)
{
// you need to instantiate a matrix object to hold the result:
matrix C;
// since this is an external function, use the accessor methods
//for(int ii=0 ; ii<A.nrow ; ii++)
for(int ii=0 ; ii<A.getRows() ; ii++)
{
// ditto for columns
for(int jj=0 ; jj<A.ncol ; jj++)
{
// here, use setElement and getElement
//p[ii][jj] = A.p[ii][jj] + B.p[ii][jj];
double element = A.getElement(ii, jj) +
B.getElement(ii, jj);
C.setElement(ii, jj, element);
}
}
// then, you need to return the result
return C;
}
</pre>
As to your PS, I use the pre tag. I think the code tag changes to a monospace font, but doesn't preserve white space. |