Solved

Overloading the ifstream operator >>

Posted on 1997-06-04
2
941 Views
Last Modified: 2006-11-17
I am writing a program and am attempting to overload the ifstream operator >>. I have done it in this form:

ifstream & operator >> (ifstream & infield, Class_name & cclass)

where i pass a class type as the second parameter.
However, for what I am doing this time, I only need to pass it a string but I have a friend who has had trouble with this before while trying to pass a pointer. Will it work in either of these ways:

ifstream & operator >> (ifstream & infield, string & instring)

or

ifstream & operator>>(ifstream & infield, string instring).

If neither of these will work, please tell me the correct syntax for passing it a string instead of a class as the second parameter.

Thank you.
Orpheus
0
Comment
Question by:orpheus
2 Comments
 
LVL 1

Accepted Solution

by:
strobert earned 50 total points
ID: 1163837
the first one:
ifstream & operator >> (ifstream & infield, string & instring)

string is a class, but even with a base type like int, just have the second arg be a ref to the type you want to instream.
0
 

Author Comment

by:orpheus
ID: 1163838
thanks, ill give it a try.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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 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 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.

786 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