Thread: Matrix operator+ problem

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Matrix operator+ problem

    I have this program that I am using to add 4*4 matrices:

    Code:
    #include<iostream>
    using namespace std;
     
    class Matrix {           
          
          double x[4][4];
     
          public:
     
          Matrix operator+(Matrix);
     
    };
     
    Matrix::Matrix operator+(Matrix mat) {
                         
                         Matrix temp;                                
                 
                         for(int i = 0; i<4; i++)
                         
                                 for(int k = 0; k<4; k++)
                         
                                         temp.x[i][k] = x[i][k] + mat.x[i][k];                     
                                 
                         return temp;
    }
    But the compiler tells me
    Matrix::x[4][4] is private within this context
    . I don't understand how x can be private to a method that I declared within the class?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You aren't defining a member function.

    Soma

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Well isn't the operator+ a member function of the class Matrix? Are operator functions not member methods of a class if declared within that class?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by phantomotap View Post
    You aren't defining a member function.
    In other words, you've got the Martix:: part in the wrong place, it should be right before 'operator'.
    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"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Change the first line of the definition from
    Code:
    Matrix::Matrix operator+(Matrix mat)
    to
    Code:
    Matrix Matrix::operator+(Matrix mat)
    That way you will actually define the operator+() you have declared in the class definition, rather than something else.

    Generally considered better to pass the argument by (const) reference as well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks all. Yeah that was the Problem.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    btw..
    Code:
    Matrix::Matrix operator+(Matrix mat)
    What does that mean to the compiler..?..(as it catches an error relating to a private member....so..this must be somewhat valid...having another meaning)

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Defines global operator+ I believe. If the compiler tolerates using the scoping operator 'inappropriately' as discussed here. But the implementation did not use public members, obviously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Class Operator Overloading.
    By Fatima Rizwan in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2010, 09:59 AM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Replies: 3
    Last Post: 04-29-2005, 02:03 AM
  4. Replies: 21
    Last Post: 04-25-2005, 07:18 PM
  5. Need a quick example! Operator overloading and matrix
    By Great Satchmo in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 10:08 PM