Overload insert/extract operator

This is a discussion on Overload insert/extract operator within the C++ Programming forums, part of the General Programming Boards category; Hi all, for our lab exercises we were asked to overload both the << and >> operator for a class ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    22

    Overload insert/extract operator

    Hi all, for our lab exercises we were asked to overload both the << and >> operator for a class we made named Phone

    From what our instructor has told us....
    Code:
    friend ostream &operator<<(ostream & out, Phone &s); 
    friend istream &operator>>(istream & in, Phone &s);
    //must be a non member function since it needs to access the private data
    Up to that part I understand why he has said that

    But I am confused as to why my compiler keeps saying
    'opeartor' : references must be initialized

    thus far I have only attempted the insertion operator, but I'm lost

    Code:
    istream &opeartor >>(istream &in, Phone &s)
    {
    	in >> s; 
    	return in; 
    }
    It would be a big help if someone can direct me on how to do this?

    e.g, in the driver we have

    Code:
    Phone phone1; 
    //should be able to do
    cout << phone1; 
    cin >> phone1;
    Please and thank you

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,205
    You have misspelled the word "operator" in your code. Compilers do not attempt to correct for misspellings.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    Code:
    istream &operator>>(istream & in, Phone &s)
    {
    	in >> s.number; 
    	return in; 
    }
    Fixed.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    However, there are two more problems in the code.

    >>friend ostream &operator<<(ostream & out, Phone &s);
    >>friend istream &operator>>(istream & in, Phone &s);

    should be

    friend ostream & operator << (ostream & out, const Phone & phone);
    friend istream & operator >> (const istream & in, Phone & phone);

    Because some parameters aren't--and shouldn't--be changed. Thus, they should be const. Also, s is not a very good parameter name. I suggest you choose something more descriptive, such as phone. This isn't math where everything is a variable of one letter.
    And the biggest problem is:

    in >> s;

    This will cause the call to be recursive without end. This is the operation you are trying to define. What, exactly, should happen when we try to read something into the phone? As cool_guy's code shows, you probably want to extract the phone number and store it into the phone. By doing this, you've defined the operation of reading a phone.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    The header that we received in our lab was simply this
    Code:
    ostream &operator<<(ostream & out, Staff &s)
    istream &operator>>(istream & in, Staff &s)
    The parameter s, like you said not that well but what I came up with so far is this
    Code:
    ostream &operator<<(ostream & out, Staff &s)
    {
    	out << s.ID; 
    	out << s.Name; 
    	out << s.Department; 
    	out << s.Position; 
    	out << s.DateJoined; 
    	out << s.Salary; 
    	return out; 
    }
    and for in >>
    Code:
    istream &operator>>(istream & in, Staff &s)
    {
    	in >> s.ID; 
    	in >> s.Name; 
    	in >> s.Department; 
    	in >> s.Position; 
    	in >> s.DateJoined; 
    	in >> s.Salary; 
    	return in; 
    }
    Because according to our instructor, we should be able to declare a variable of type Staff in the driver and then be able to
    Code:
    Staff somestaff; 
    cout << somestaff << endl; 
    cin >> somestaff;
    Is there any suggestions on how to properly format the way the user can enter the information? Any suggestions is appreciated.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Elysia View Post
    should be

    friend ostream & operator << (ostream & out, const Phone & phone);
    friend istream & operator >> (const istream & in, Phone & phone);
    Not quite. the istream cannot be const. Reading from a stream inherrently changes its state since the next read does not read the same thing, therefore that operation is not const.

    He was only missing the first const on the Phone reference. I.e. You have the top one correct, he has the bottom one correct.
    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"

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. Overload False Operator
    By kenryuakuma in forum C# Programming
    Replies: 6
    Last Post: 11-25-2009, 09:38 PM
  3. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 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