Solved

operator overloading

Posted on 2004-04-12
4
732 Views
Last Modified: 2008-02-26
//////////////////operator overloading//////////////////////////////

inline istream& operator >>(Date& dd)
{
      Date indate=dd;
}

//////////////////////////error//////////////////////
c:\proj\date.h(50) : error C2805: binary 'operator >>' has too few parameters
Error executing cl.exe.

Date.obj - 1 error(s), 0 warning(s)


I am facing this error of too few parameters very frequently. If I checkup with the syntax I dont find anything wrong. Could you please explain.
0
Comment
Question by:rmvprasad
4 Comments
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10810472
I/o operators are free functions
make it inline istream& operator >>(istream& st, Date& dd)

http://www.cs.duke.edu/csed/cplus/overload/
0
 
LVL 4

Accepted Solution

by:
booki earned 20 total points
ID: 10810473
rmvprasad,

inline istream& operator>>(istream &input, Date &dd) {
     Date indate=dd;
    ....
    return input;
}

b.
0
 

Author Comment

by:rmvprasad
ID: 10810578
I got the idea about operator overloading, but one problem is not solved. In both istream or ostream operators the value is accepted and finally ended with a return statement. If a return statement occurs how can I accept a value using cin>> function. Actually I am supposed to write a code for over loading to accept a datatype Date into a Date variable. Couls you solve it for me.
0
 
LVL 1

Expert Comment

by:ravenscr98
ID: 10810664
For the input operator >>, the first argument (the left operand to >>) is a reference to the input file stream object.  The second argument (the right operand to >>) is a reference to the object that will hold the input value.  Because the right operand is passed by reference, any change made to the parameter in the function will affect the argument to the function, which in this case is the right operand that will hold the input value.  The return statement returns the input stream object, which allows you to input multiple values in one statement, such as cin >> x >> y;  .

Your overloaded operator will work with cin because cin is an instance of a class that has istream as a superclass.  Thus it can be passed to the istream & parameter.

To write your overloaded input operator, do as bookj suggested.

inline istream& operator>>(istream &input, Date &dd) {
    // read data from input
    // and store it in dd

      ....

    // return the istream object
    return input;
}

The code

Date d;
cin >> d;

will call your overloaded input operator.  The left operand cin will be passed to the argument input by reference.  The right operand d will be passed to the argument dd by reference.  Within the operator definition, you read data from input and modify dd.  Because dd is a reference parameter, any changes in dd actually happen to the right operand d.

Overloading the output operator << is done in a similar fashion.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now