Solved

Overloading '<<' for class BookLoan

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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