Solved

operator overloading

Posted on 2004-04-12
4
738 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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