problems with matrix mult. method

This is a discussion on problems with matrix mult. method within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm working on a simple 3d engine (probably just a starfield simulation) but am running accross problems in ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Question problems with matrix mult. method

    Hey all, I'm working on a simple 3d engine (probably just a starfield simulation) but am running accross problems in my operator* code (this is for 4x4 matrices):

    Code:
     M4x4 M4x4::operator*(const M4x4 &mat)
      {
        M4x4 product;
        
        for(int i = 0; i < 4; i++)
          for(int j =0; j < 4; j++)
            product.setValue(i,j, (float)(this->matrix[i][1] * mat.getValue(1,j)
            + this->matrix[i][2] * mat.getValue(2,j)
            + this->matrix[i][3] * mat.getValue(3,j)));
        return product;
      }
    where setValue set's matrix[i][j] to a float value and getValue returns the float, matrix[i][j].

    I'm getting the following errors in Metrowerks Codewarrior IDE 4.1:

    Code:
    Error   : cannot pass const/volatile data object to non-const/volatile member function
    M4x4.cpp line 26   product.setValue(i,j, (float)(this->matrix[i][1] * mat.getValue(1,j)
    
    Error   : cannot pass const/volatile data object to non-const/volatile member function
    M4x4.cpp line 27   + this->matrix[i][2] * mat.getValue(2,j)
    
    Error   : cannot pass const/volatile data object to non-const/volatile member function
    M4x4.cpp line 28   + this->matrix[i][3] * mat.getValue(3,j)));
    
    Error   : cannot pass const/volatile data object to non-const/volatile member function
    M4x4.cpp line 38   sum.setValue(i,j, matrix[i][j] + mat.getValue(i,j));
    Which doesn't make a whole lot of sense to me.

    is there something wrong with this code? Maybe it's something elsewhere in my code causing this particular section to mess up?

    Help please.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    product.setValue(i,j, (float)(this->matrix[i][1] * mat.getValue(1,j)
    Here (line 26) you're passing a constant variable to a function which take a non-constant variables as an argument. This is invalid, since a function with non-constant arguments are allowed to modify those, but you're passing something that isn't allowed to be modified.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing object references to functions
    By circuitbreaker in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2008, 11:21 AM
  2. Simple operator overloading issue
    By Desolation in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2007, 08:56 PM
  3. Replies: 1
    Last Post: 03-08-2006, 05:47 PM
  4. Replies: 1
    Last Post: 03-06-2006, 06:57 PM

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