overloading operator help

This is a discussion on overloading operator help within the C++ Programming forums, part of the General Programming Boards category; k, this board is my only available "lab" for a univeristy level course, so please help if u can... i ...

  1. #1
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12

    overloading operator help

    k, this board is my only available "lab" for a univeristy level course, so please help if u can...

    i am pretty sure i simply do not understand classes yet, but my stupid assignment is asking me to overload operators and i just cant make the compiler happy. after following the instrcutor's example, i get stuck with odd compiler errors that dont make any sense (go figure).

    if anyone can point out what i am doing wrong, i would appreciate it...

    Code:
    #include <iostream.h>
    
    class point
    {
    	private:
    		float x;
    	public: 
    		point()	{ cout<<"Enter a fraction: "; cin>>x; };
    		point operator +(point a, point b); // point a, point b
    };
    float point operator +(point a, point b)
    {
    		point result;
    		result.x=a.x+b.x;
    		return result;
    }
    void main()
    {
    	point a;
    	point b;
    	a.read();
    	b.read();
    	point r1=a+b;
    }
    Code:
    fractions_source.cpp(9) : error C2804: binary 'operator +' has too many parameters
    fractions_source.cpp(11) : error C2143: syntax error : missing ';' before '+'
    fractions_source.cpp(11) : fatal error C1004: unexpected end of file found
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    float point operator +(point a, point b)
    should be
    Code:
    point operator +(point a, point b)
    and
    Code:
    point operator +(point a, point b); // point a, point b
    should be
    Code:
    friend point operator +(point a, point b); // point a, point b
    because the function returns a point, not a float, and because the function should not be part of the point class.
    Last edited by jlou; 09-29-2004 at 08:10 PM.

  3. #3
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12
    that worked, thanx jlou
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  4. #4
    Registered User GuyOnDrugs's Avatar
    Join Date
    Mar 2002
    Posts
    8
    Not to spam in this thread, but I just completed the exact same assignment for my CS-230 Computer Science Fundamentals class two weeks ago. Such a small world it is

  5. #5
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12
    230? i'm only in 131...

    i hope u have a lab, cuz my school feels it's unnecessary for some odd reason
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Your example code requires you to "Enter a fraction" when calling the operator+ function because of the temp result object that is created. This input is thrown away in this case and probably should not be a part of your constructor code, which I suspect may be the case since you mention a read function but do not include that code in your sample.

    Here is one more way for comparisons sake that does things a little differently, I have added the friend ostream& operator<< to make output easy and also the read function:

    Code:
    #include <iostream>  // The iostream.h header is deprecated, use this newer version
    // Due to the above header change you will also need to deal with namespaces,
    // for this simple example's sake just add this following line
    using namespace std;
    
    class point
    {
    private:
        float x;
    public: 
        point()	{}
        void read() { cout<<"Enter a fraction: "; cin>>x; }  // Moved from constructor
        point operator +(const point& b);
        friend ostream& operator<<(ostream& os, const point& pt);
    };
    
    ostream& operator<<(ostream& os, const point& pt)
    {
        return os << pt.x << endl;
    }
    
    point point::operator +(const point& b)
    {
        point result;
        result.x = x + b.x;
        return result;
    }
    
    int main() // main returns an int always
    {
        point a;
        point b;
        a.read();
        b.read();
        point r1=a+b;
        cout << a << b << r1;
        return 0;
    }
    Example output:

    Code:
    Enter a fraction: 6.7
    Enter a fraction: 4.9
    6.7
    4.9
    11.6
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    hk_mp5kpdw---In your example of overloading the + operator as a member function of the class, I believe you also need to add a public accessor function. This is because the desired member variable of the parameter you are passing to the current objects operator is declared with private access and therefore is not available to other objects of the class directly.

    Also, since you don't have any newlines, explicitly or implicitly by use of keyword endl, the output will all be on one line rather than on separate lines as posted. Not that that has anything to do with operator overloading, but...
    Last edited by elad; 09-30-2004 at 09:29 AM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806

    Wink

    Quote Originally Posted by elad
    hk_mp5kpdw---In your example of overloading the + operator as a member function of the class, I believe you also need to add a public accessor function. This is because the desired member variable of the parameter you are passing to the current objects operator is declared with private access and therefore is not available to other objects of the class directly.
    Works for me. I tested the code out as posted using MS Visual Studio and it ran as shown in the example output. As a member function of the point class, operator+ can access the member variables of that class. A public accessor function would work but I don't think it is needed.

    Quote Originally Posted by elad
    Also, since you don't have any newlines, explicitly or implicitly by use of keyword endl, the output will all be on one line rather than on separate lines as posted. Not that that has anything to do with operator overloading, but...
    Code:
    ostream& operator<<(ostream& os, const point& pt)
    {
        return os << pt.x << endl;
    }
    ...
    cout << a << b << r1;
    The endl is there, just not where you thought it was.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    hk_mp5hpdw--I stand corrected on both points. Somehow I got into the habit of using public accessor and mutator functions automatically on private member varaibles for any object other than the current object. I've been able to confirm your report that objects of a given class can directly access private members of objects of the same class wihtout needing the public accessor/mutators. That means I can simplify my code writing style somewhat. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 05:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 05:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM

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