overload *

This is a discussion on overload * within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to overload * for matrix multiplications. For some reason the overload doesn't work for more than ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    overload *

    Hi
    I am trying to overload * for matrix multiplications. For some reason the overload doesn't work for more than two consecutive operations (ie. M*T*V).
    Code:
    // Overload * for ob1 
    
    matrix_square operator*(const matrix_square &left, const matrix_square &right)
    {
    
      int i;
      matrix_square temp;
    
    
      for(i = 0; i < Grids; i++){
    
        temp.ptr00[i] = left.ptr00[i]*right.ptr00[i] + left.ptr01[i]*right.ptr10[i] + left.ptr02[i]*right.ptr20[i];
        temp.ptr01[i] = left.ptr00[i]*right.ptr01[i] + left.ptr01[i]*right.ptr11[i] + left.ptr02[i]*right.ptr21[i];
        temp.ptr02[i] = left.ptr00[i]*right.ptr02[i] + left.ptr01[i]*right.ptr12[i] + left.ptr02[i]*right.ptr22[i];
    
        temp.ptr10[i] = left.ptr10[i]*right.ptr00[i] + left.ptr11[i]*right.ptr10[i] + left.ptr12[i]*right.ptr20[i];
        temp.ptr11[i] = left.ptr10[i]*right.ptr01[i] + left.ptr11[i]*right.ptr11[i] + left.ptr12[i]*right.ptr21[i];
        temp.ptr12[i] = left.ptr10[i]*right.ptr02[i] + left.ptr11[i]*right.ptr12[i] + left.ptr12[i]*right.ptr22[i];
    
        temp.ptr20[i] = left.ptr20[i]*right.ptr00[i] + left.ptr21[i]*right.ptr10[i] + left.ptr22[i]*right.ptr20[i];
        temp.ptr21[i] = left.ptr20[i]*right.ptr01[i] + left.ptr21[i]*right.ptr11[i] + left.ptr22[i]*right.ptr21[i];
        temp.ptr22[i] = left.ptr20[i]*right.ptr02[i] + left.ptr21[i]*right.ptr12[i] + left.ptr22[i]*right.ptr22[i];
      }
      temp.zeros();
    
    
      return temp;
    }
    where ptr20 etc are elements of a matrix. Any advise please.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what is the error?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Well that code looks fine, assuming it is declared as static or friend. Define "doesn't work".

    What is the definition of Grids, and zeros(), and what other code from matrix_square can you post that might help?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Is Grids a global var? Not too good solution for C++

    All your matrixes are 3x3? Have you heared about vectors?

    ptrXY variables arritating me a little.

    temp.zeros(); - hmmm, are you zeroing all you calculated results before returning them? Or it is just a bad naming?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    It's working fine now for some strange reason. Do tell me about vectors is it from standard C++ library?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overload Operators
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 11:13 AM
  2. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 02:03 PM
  3. overload new and delete
    By Roaring_Tiger in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2003, 07:48 PM
  4. C++ Operator Overload Question
    By cworld in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2002, 06:17 PM
  5. Overload (+) operator
    By kit in forum C++ Programming
    Replies: 8
    Last Post: 10-23-2001, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21