Solved

Overloading '<<' for class BookLoan

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

937 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

3 Experts available now in Live!

Get 1:1 Help Now