binary '+' : no operator found which takes a right-hand operand

I am trying to use operator+ overloading to add to matrices classes. It is on the line rslt = m1+m2 where rslt, m1, m2 are alll matrix classes.
Here is the code:
int main(int argc,char *argv[])
{
matrix rslt;

matrix m1;
m1.initMatrix(10,10,5,1);
m1.print();

matrix m2;
m2.initMatrix(10,10,10,2);
m2.print();
// this works
m1 = m2;
// gives error here
rslt = m1+m2;
}
matrix matrix::operator=(const matrix & rhs)
{
int iter = rhs.getIterations();
this.setIteations(iter);
return *this;
}
matrix matrix::operator+(const matrix & b)
{
int ** result;
int rows = this.getRowSize();
int cols = this.getColSize();
int seed = this.getSeed();

matrix *rslt = getResultMatrix(rows, cols, seed);

for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
rslt.m[i][j] = this.m[i][j]+b.m[i][j];
}
}
return rslt;
}

When I add using operator+ overload, it gives error. Please can someone tell me what is wrong?
A matrix is a typedef for an int **?
Yes kind of. int ** result and int**m are defined to be 2d matricies in class matrix.
I am not using rslt. I am using 'm' to stroe the data after addition.
m1, m2, rslt are instances of class matrix, on which the operation rslt=m1+m2 gives error.
This is the class definition:
class matrix
{
......
......
int **m;
int **result;
matrix matrix::operator+(const matrix & rhs);
........
....
}
Yes, OK, I see where I misread it.

Your matrix should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class matrix
{
private:
  // These are all the data you need to store the matrix
  int**    m;
  unsigned rows;
  unsigned cols;

  // Useful functions to maintain the matrix data
  void init(unsigned nrows, unsigned ncols);  // Create new m[rows=nrows][cols=ncols]
  void zero();  // Clear the matrix (to zeros)
  void copy(int** src);  // Copy source matrix of same size to this matrix
  void free();  // delete[][] the matrix data.

public:
  matrix(unsigned nrows=0, unsigned ncols=0);
  matrix(const matrix& source);
  ~matrix();

  matrix& operator=(const matrix& source);

  ...

  unsigned rowcount() const { return rows; }
  unsigned colcount() const { return cols; }

  ...

  int& operator()(unsigned row, unsigned col);
  const int& operator()(unsigned row, unsigned col) const;

  ...

  matrix operator+(const matrix& rhs);
  matrix& operator+=(const matrix& rhs);

  ...
};

matrix::matrix(unsigned nrows, unsigned ncols)
{
  init(nrows, ncols);
  zero();
}

matrix::matrix(const matrix& source)
{
  init(source.rows, source.cols);
  copy(source.m);
}

matrix::~matrix()
{
  free();
}

matrix& matrix::operator=(const matrix& source)
{
  free();
  init(source.rows, source.cols);
  copy(source.m);
}

matrix matrix::operator+(const matrix& rhs)
{
  matrix result(*this);
  result += rhs;
  return result;
}

matrix& matrix::operator+=(const matrix& rhs)
{
  if ((rows != rhs.rows) || (cols != rhs.cols))
    throw std::domain_error( "matrix::operator+(): lhs and rhs must have the same dimensions" );

  for (unsigned r=0; r<rows; r++)
  for (unsigned c=0; c<cols; c++)
    m[r][c] += rhs.m[r][c];

  return *this;
}

I have no idea what you are doing with getSeed() and the like.

Remember that this is a pointer:
this->m[i][j]

Hope this helps.
Topic archived. No new replies allowed.