Solved

Overloading the ifstream operator >>

Posted on 1997-06-04
2
946 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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

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