Like Tree1Likes
  • 1 Post By phantomotap

Matrix operator+ problem

This is a discussion on Matrix operator+ problem within the C++ Programming forums, part of the General Programming Boards category; I have this program that I am using to add 4*4 matrices: Code: #include<iostream> using namespace std; class Matrix { ...

  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
    4,415
    O_o

    You aren't defining a member function.

    Soma
    Dontgiveup likes this.

  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,308
    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,646
    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, Sunshine, 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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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, 10:59 AM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 02:14 PM
  3. Replies: 3
    Last Post: 04-29-2005, 03:03 AM
  4. Replies: 21
    Last Post: 04-25-2005, 08: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, 11:08 PM

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