Solved

Overloading '<<' for class BookLoan

Posted on 1997-05-29
2
194 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
2 Comments
 

Accepted Solution

by:
rlayton earned 100 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

10 Experts available now in Live!

Get 1:1 Help Now