[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Overloading '<<' for class BookLoan

Posted on 1997-05-29
2
Medium Priority
?
253 Views
Last Modified: 2010-04-10
Hello All,
 
I am having a very strange error when compiling the bookloan.cpp file
when  I write the following code:
 
       // File output
       //
       // Receive: An fstream object, OutStr
       //          A BookLoan object, aLoan
       // Output:  The values of aLoan's data members with         //a space between them
       //          and no end of line character.
       // Return:  OutStr
fstream &operator<<(fstream &OutStr, const BookLoan &aLoan)
 {
       OutStr << aLoan.FirstName_;
         return OutStr;
 }
 
 I get compiler errors saying the member FirstName_ is not accessable and > also the object aLoan is never used. This is very confusing especially when  there is an example of this  in the date class of EXACTLY the same
format.

 Also I have manage to  sucessfully overload '>>' for fstream so what is  wrong with this ! I would have to say I've checked all the obvious ideas like checking that fstrean is declaired as a friend in the header file,
 
  help  greatfully appreciated
 
 thanks
0
Comment
Question by:dklemitz
[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
2 Comments
 

Accepted Solution

by:
rlayton earned 200 total points
ID: 1163737
It appears that  FirstName is a private or protected member of the BookLoan class, make sure it's a public member and that should take care of both errors.
0
 

Author Comment

by:dklemitz
ID: 1163738
Thanks for your prompt response.

While I understand FirstName_ is a private data member of the class I know that to access these members I can say in other parts of the code:
               ObjectName.MemberFunction

I still think this is correct for the following reasons

1.  I sucessfully overloaded the input operator '>>' for an fstream object and the same object as follows with no complier errors:

// File input
//
// Receive: An fstream object, InStr
//          A BookLoan object, aLoan
// Input:   A Borrower, a return date and a title
// Return:  aLoan, with the input values stored in its data      //          members InStr

      // Strip leading spaces.
      // Read the First name
      //Use GetLine to include the end of line character

fstream & operator >> (fstream &InStr, BookLoan &aLoan)
{

Strings

      aFirstName;

      while (InStr.peek() == ' ' )
             InStr.get();

            aFirstName.GetLine(InStr, 100, ' ');
               aLoan.FirstName_ = aFirstName;
return InStr;
}

The other reason is that I have seen the output  operator '<< ' overloaded in the class Date you may have access to for an fstream object.  If you have access to a similar class I think you will see what I mean.

So to recap the data member is private in both cases so I'm still at a difficult point trying to understand the difference.

I don't mind some coresspondance

regards

David Klemitz (es00749@ozemail.com.au)
0

Featured Post

Independent Software Vendors: 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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 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.
Suggested Courses

650 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