?
Solved

Overloading '<<' for class BookLoan

Posted on 1997-05-29
2
Medium Priority
?
251 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 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