S
S
Saveli Tomak2017-10-01 18:10:12
C++ / C#
Saveli Tomak, 2017-10-01 18:10:12

How to overload operator * for own class?

matrix.hpp

class Matrix
{
public:
  Matrix(int rows, int columns, const double* elems);
  ~Matrix();

  Matrix* operator*(Matrix& matrix);

  double det();
  Matrix* invert();
  Matrix* transpose();
};

matrix.cpp
Matrix* Matrix::operator*(Matrix& matrix)
{
//
}

main.cpp
int main(int argc, char** argv)
{
  double d[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};

  Matrix* m = new Matrix(3, 3, d);

  Matrix* m1 = new Matrix(3, 3, d);

  Matrix* m2 = m * m1;

  return EXIT_SUCCESS;
}

Error text: main.cpp:16:5: error: invalid operands of types 'Matrix*' and 'Matrix*' to binary 'operator*'
m * m1;

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2017-10-01
@aresouji

You are trying to multiply pointers. Dereference them first.
Or make the operator not a member of the class, with the signature (Matrix*, Matrix*).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question