0

I'm currently trying to multiply one of the matrices in my program with a constant but as there is no existing method to do that I guess I have to overload an operator or such. But as I have already overloaded it the " * " operator once. It also needs to take the value or more specifically the constant from the left side and not from the right. How do I proceed?

All help is appreciated!

The overloaded * operator

matrix operator * (matrix const arg){

    double sum = 0;
    int x = 0;
    int rowY = 0;
    int rowX = 0;
    this->matrix_array_multiply = new double[row*arg.col];
    for (int position = 0; position < row*arg.col; position++, x++){

        if (arg.matrix_array[x*arg.row] == (arg.matrix_array[arg.row*arg.col])){

            //If last number in second matrix, reset these values and proceed with next row of Y-values.

            x = 0;
            rowY++;

        }

        for (int y = 0; y < arg.row; y++, rowX++){

            sum = sum + (matrix_array[y + (rowY*col)]) * (arg.matrix_array[x + (rowX*arg.col)]);

        }

        matrix_array_multiply[position] = sum;
        rowX = 0;
        sum = 0;

    }

    matrix new_matrix_multiply(matrix_array_multiply, row, arg.col); //Create new instance of new matrix.
    return new_matrix_multiply; //Return new matrix.

}

Usage of the different matrices and operators:

int main () {

double a[] =  { 3, 0, 3, 4,
                3, 4, 2, 4,
                5, 3, 2, 1 };

double b[] =  { 6, 3, 5, 7,
                9, 8, 6, 4,
                6, 5, 3, 1 };

double c[] =  { 1, 2, 3, 4,
                5, 6, 7, 8,
                9, 2, 1, 1,};

double d[] =  { 6, 5, 4, 3,
                2, 1, 0, 1,
                2, 3, 4, 5,
                6, 7, 8, 9};

double e[] = { 1, 2, 1,
               3, 5, 7,
               9, 7, 3};

matrix matrix1(a, 3, 4); //Instance of the class matrix: array, rows, columns.
matrix matrix2(b, 3, 4);
matrix matrix3(c, 3, 4);
matrix matrix4(d, 4, 4);
matrix matrix5(e, 3, 3);

matrix matrix6 = (matrix1 + matrix2);
matrix matrix7 = (matrix2 - matrix1);
matrix matrix8 = (matrix3 * matrix4);
matrix matrix9 = ~matrix5;
matrix matrix10 = (5.7 * matrix5); // Error: no operator "*" matches these operands, operand types are: double * matrix 
}

Note: I've just started to learn c++ and this is part of a homework.

1 Answers1

2
matrix matrix10 = (5.7 * matrix5);

For this to work, you've to define a free function with this signature:

matrix operator*(double c, matrix const & m) //non-member function
{
   //your code
}

And likewise, you would like to define this also:

matrix operator*(matrix const & m, double c)
{
    return c * m; //call the other overload!
}
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Why is the non-member needed? – Abhishek Bansal Nov 28 '13 at 18:17
  • 1
    @AbhishekBansal: The first one needs to be non-member because of how C++ works. The second one can be made member, but that would be a bad design! – Nawaz Nov 28 '13 at 18:18
  • why can't it be like the way in my answer? (I'm sure there must be something wrong but I don't get it) – Abhishek Bansal Nov 28 '13 at 18:22
  • @AbhishekBansal: Your code will work for `(m * 10)`, not for `(10 * m)` in which case the left operand is `double`, so the member function cannot be invoked, it is how C++ is defined. – Nawaz Nov 28 '13 at 18:23
  • @AbhishekBansal: As for why the second overload in my answer should be a free function, see my answer here for detailed explanation: http://stackoverflow.com/questions/7376554/functions-with-return-values-c – Nawaz Nov 28 '13 at 18:26
  • How do I reach the "arguments" used with the matrix class? For example if I would like to do something similiar to "this->matrix_array_multiply = new double[arg.row*arg.col];" – user3046795 Nov 28 '13 at 19:51
  • @user3046795: Make the first function `friend` of the class, so that it could access the private parts. Better you design the class, in a way, such as exposing appropriate public functions, that you don't have access the private parts. – Nawaz Nov 28 '13 at 20:13