Overloading operator+

This is a discussion on Overloading operator+ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight Now, one possible problem lies with this member function: Code: double **getTerms() const; The problem is ...

  1. #16
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Quote Originally Posted by laserlight View Post
    Now, one possible problem lies with this member function:
    Code:
    double **getTerms() const;
    The problem is that you want to call it from a non-const reference to Matrix. You should actually use const overloading:
    Code:
    double** getTerms();
    const double* const * getTerms() const;
    I have never heard of const overloading, but I get a basic idea from the name and I will go look it up very soon, but are you saying I should replace my declaration of
    Code:
    double** getTerms() const
    with
    Code:
    const double* const * getTerms() const;
    ? I tried replacing them in the .h and .cpp and still got a bunch of errors, but I'm thinking I used it wrong.

    Quote Originally Posted by laserlight View Post
    By the way, your destructor does not correctly destroy the member array. It fails to destroy each dynamic array of double. Notice that my operator+ implementation takes the first argument by value, not by reference.
    Thanks, you're right. I need to fix that.

  2. #17
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    And also, wouldn't
    Code:
    #include <iostream>
    using std::ostream
    (and I guess any other one's I would need in other scenarios)
    be necessary in Matrix.h so that I can declare
    Code:
    friend ostream &operator<<(ostream &, const Matrix &);
    ?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Yes, but you can also declare:
    Code:
    friend std::ostream &operator<<(std::ostream &, const Matrix &);
    By the way, std::ostream is actually from <ostream>. You only need to #include <iosfwd> in the header if you only declare functions in the header.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Just to clarify, was there anything wrong with where and/or how I declared and implemented the operator+ and operator+= functions in post 14? I know you pointed out getTerms(), but I'd like to know if I was declaring/implementing those functions correctly as non member non friends so I can know if that's what was causing so many errors or if it was getTerms() and/or something else.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Yes, one correction to make is that operator+ should still take its first argument by value (or const reference) and return by value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    I hate to state this again, but I am still having trouble with moving the operator functions to non-member non-friend. I moved them back to global friend functions so that I could test other parts of the code. It all seems to work fine with the functions as global friends like this
    Code:
    class Matrix
    {
    	friend Matrix operator+=(Matrix &, const Matrix &);	//overload +=
    	friend Matrix operator+(Matrix, const Matrix &);	//overload +
    	friend Matrix operator*=(Matrix &, double);	//overload *=
    	friend Matrix operator*(Matrix, double);	//overload *
    	friend Matrix operator/(Matrix, double);	//overload /
    	friend std::ostream &operator<<(std::ostream &, const Matrix &);	//print a matrix
    But as soon as I move one or all of them to non member non friend like so

    Code:
    Matrix operator+=(Matrix &, const Matrix &);	//overload +=
    	 Matrix operator+(Matrix, const Matrix &);	//overload +
    	 Matrix operator*=(Matrix &, double);	//overload *=
    	 Matrix operator*(Matrix, double);	//overload *
    	 Matrix operator/(Matrix, double);	//overload /
    
    class Matrix
    {
    	friend std::ostream &operator<<(std::ostream &, const Matrix &);	//print a matrix

    I get a huge amount of syntax errors all over the code. I don't see why it's not working.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Hold on, did you forward declare the class? What are the error messages?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Oh wow, no I did not forward declare the class
    That was the problem. It compiles fine now. Thanks for the reminder.

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    += is usually a member. + should be the non-member. Also, move the operators after the class, then you don't need the forward declaration of the class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by SterlingM
    It compiles fine now.
    Good to hear. A comment on your getTerms() member function though: by returning a double**, you are effectively exposing the implementation. It may be better to overload operator() to take two arguments. This way, instead of writing additive1.getTerms()[r][c], you can write additive1(r, c). If you prefer to write additive1[r][c], then you would use a proxy class such that operator[] for the Matrix class returned a proxy object whose operator[] is called to return the double. You will still need const overloading since the non-const version should return a reference.

    Quote Originally Posted by CornedBee
    += is usually a member. + should be the non-member.
    In this case, they both can be non-member non-friend functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    I am having a problem with overloading operator(). I have three statements along the lines of
    Code:
    (r, c) = theTerm;	//assign
    The compiler warns me of a loss of precision from double to int which confuses me because the only int's involved are the parameters that don't get assigned to anything. In my setTerms() function, the variable c (in the inner for loop) is set to whatever I input as a term. Like if I put a 3 into the first element, c will go from 0 to 3 then to 4 at the increment. Or if I constantly enter 1 it will stay at 2.
    Code:
    double &Matrix::operator()(int r, int c) {
    	return terms[r][c];
    }	//END OPERATOR()
    
    double Matrix::operator()(int r, int c) const {
    	return terms[r][c];
    }	//END OPERATOR()CONST
    I was using getTerms() to return, but I changed it to just terms[r][c] and I still get the same results. My setTerms() function is
    Code:
    //user inputs terms to be put into the matrix
    void Matrix::setTerms() {
    	cout<<"**********Matrix "<<getName()<<"**********"<<endl;	
    	for(int r=0;r<getRows();r++) {
    		for(int c=0;c<getCols();c++) {
    			double theTerm;
    			cout<<"Enter the term for element ["<<r<<", "<<c<<"]."<<endl;
    			cin>>theTerm;	//take in theTerm
    			(r, c) = theTerm;	//assign	COMPILER WARNING FOR LOSS OF DATA DOUBLE TO INT
    		}	//end inner for
    	}	//end double for
    }	//END SETTERMS()
    I know I've asked a lot of questions about this code, but I am so lost on how c is getting set to theTerm in setTerms(). Also, this(r,c) doesn't work. this->(r,c) doesn't work. I can get it to work by using getTerms()[][], but I'd like to get more comfortable with operator overloading and know how to make it work this way.
    Last edited by SterlingM; 01-13-2010 at 03:32 PM.

  12. #27
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Okay well instead of
    Code:
    (r, c) = theTerm
    I used
    Code:
     this->operator()(r, c) = theTerm;
    This compiles and passes tests.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    You can also use (*this)(r, c).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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