Solved

operator overloading

Posted on 2004-04-12
4
733 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

912 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

16 Experts available now in Live!

Get 1:1 Help Now