Solved

operator overloading

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

822 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