?
Solved

operator overloading

Posted on 2004-04-12
4
Medium Priority
?
739 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 60 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

752 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