Solved

Overloading the ifstream operator >>

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now