You are using a version without Ads of this website. Please, consider donating:

Hi Everyone,

First post, I'm trying to set up my functions and perform some operation overloading so that I can +,-,==,!=,* two matricies. I have encountered a problem at the first operation overload: addition.

My program compiles but gives a random number as the addition of two matricies:

i.e. 10+5 = 6122608

This is just the first step: I am then needed to run this in an MPI environment, so I will likely be back soon with more questions!

Thanks for any help,

Martyn
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108`` ``````#include #include #include using namespace std; class matrix { int row; int col; int **array; public: matrix(int,int); //constuctor matrix(const matrix&); //constructor to copy a matrix ~matrix(); //destructor void setElement(int r, int c, int e); int getElement (int r, int c); void setColumn (int c, int a[]); int* getColumn (int c); void setRow (int r, int a[]); int* getRow (int r); matrix operator+(const matrix&); }; matrix::matrix(int r, int c){ //defines the constructor to create a new matrix row = r; col = c; array = new int*[row]; for (int i=0; i
Here is the main code i'm running to test it:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include "2.h" using namespace std; int main() { int r = 4; int c = 4; matrix matrixA(r,c); matrix matrixB(r,c); matrix matrixC(r,c); for (int i=0; i

result:
matrixA
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

matrixB
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

10027176 10027008 10027008 16
11 3 3 3
14 3 3 3
17 3 3 3

Your matrix class needs a copy assignment operator.
http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29
Ahhhhhhh I see, I will try that now, so I need;

matrix operator=(const matrix&);

Thanks. First Timers *sigh*!
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: