Trouble with overloading Insertion Operator

This is a discussion on Trouble with overloading Insertion Operator within the C++ Programming forums, part of the General Programming Boards category; Hey. I'm coding an abstract data type for a rational number. I have been attempting to code the operator overload ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    21

    Trouble with overloading Insertion Operator

    Hey. I'm coding an abstract data type for a rational number. I have been attempting to code the operator overload for insertion, but when doing so i recieve errors telling me that the objects members are private (and thus, cannot be accessed) despite it being a friend function. If someone may have any ideas about what may be wrong here, I would of course appreciate it

    ##FUNCTION DEFINITION FOR OVERLOAD##
    Code:
    ostream& operator <<(ofstream& outs, const rational& temp){
    	if (!temp.positive)
    	outs << "-";
    	outs << temp.ratNum[0] << "/" << temp.ratNum[1];
    	return outs;
    }
    ##HEADER FILE W/ CLASS DEFINITION##
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    const int size = 2;
    class rational{
    
    	friend rational operator+(const rational&, const rational&);
    	friend rational operator-(const rational&, const rational&);
    	friend rational operator*(const rational&, const rational&);
    	friend rational operator/(const rational&, const rational&);
    	void friend subtractRational(rational&, rational&, rational&);
    	void friend addRational(rational&, rational&, rational&);
    	void friend multRational(rational, rational, rational&);
    	void friend divRational(rational, rational, rational&);
    	void friend lcd (rational&, rational&);
    	void friend gcf(rational&);
    	friend ostream& operator<<(ostream&, const rational&);
    	friend istream& operator>>(istream&, const rational&);
    
    public:
    	void output();
    	rational operator =(const rational&);
    	rational(int);
    	rational(void);					 // void constructor
    	rational(int, int); 	// constructor for creating rational number
    	~rational();				// destructor
    	rational(const rational&);
    
    private:
    	int *ratNum;
    	bool positive;
    };

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The signature says:

    Code:
    friend istream& operator>>(istream&, const rational&);
    Surely you want to modify the object you are inputting to?

    Another problem with operator<<:

    Code:
    //friend declaration:
    friend ostream& operator<<(ostream&, const rational&);
    
    //implementation
    ostream& operator <<(ofstream& outs, const rational& temp) {...}
    Naturally that function isn't a friend of rational.

    I also hope ratNum, which is an array of two integers (?), is not going to be dynamically allocated?
    Last edited by anon; 01-23-2010 at 05:24 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by anon View Post
    The signature says:

    Code:
    friend istream& operator>>(istream&, const rational&);
    Surely you want to modify the object you are inputting to?

    I also hope ratNum, which is an array of two integers (?), is not going to be dynamically allocated?
    edit: thanks a lot was able to make a lot of progress due to the things you pointed out. Please forgive my lack of complete understanding of this subject - I am enrolled in a course where we will be studying these things in the future, but I am looking ahead at future material due to eagerness.
    Last edited by adamdavis; 01-23-2010 at 06:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator new overloading and template functions
    By krappa in forum C++ Programming
    Replies: 40
    Last Post: 05-28-2008, 08:29 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Operator Overloading
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2003, 11:29 PM
  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