Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:34:07+00:00 2026-05-26T15:34:07+00:00

I am having a problem with friend functions. I figure this is the only

  • 0

I am having a problem with friend functions.

I figure this is the only part of the code needed.. My problem is with this function. It says the problem is with the first line, but I don’t know how accurate that is.

friend ostream & operator << (ostream & b, Book & a)
    {
    b.setf(ios::fixed | ios::showpoint);
    b.precision(2);
    b << "Title     :  \"" << a.title << "\"\n"
    << "Author      : \"" << a.author << "\"\n"
    << "Price       : $" << a.price / 100.0 << endl
    << "Genre       : " <<a.genre << endl
    << "In stock? " << (a.status ? "yes" : "no") << endl
    << endl;
    return b;
    }

I get the errors :
lab10.cpp:95: error: can’t initialize friend function âoperator<<â

lab10.cpp:95: error: friend declaration not in class definition

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:34:07+00:00Added an answer on May 26, 2026 at 3:34 pm

    You have to specify the function is friend of which class. You either put that function in the class declaration:

    class Book{
    ...
      friend ostream & operator << (ostream & b, Book & a)
        {
        b.setf(ios::fixed | ios::showpoint);
        b.precision(2);
        b << "Title     :  \"" << a.title << "\"\n"
        << "Author      : \"" << a.author << "\"\n"
        << "Price       : $" << a.price / 100.0 << endl
        << "Genre       : " <<a.genre << endl
        << "In stock? " << (a.status ? "yes" : "no") << endl
        << endl;
        return b;
      }
    };
    

    The other way is to declare it as a friend inside the class, and define it in some other place:

    class Book{
    ...
        friend ostream & operator << (ostream & b, Book & a);
    };
    

    …

    // Notice, there is no "friend" in definition!
    ostream & operator << (ostream & b, Book & a)
        {
        b.setf(ios::fixed | ios::showpoint);
        b.precision(2);
        b << "Title     :  \"" << a.title << "\"\n"
        << "Author      : \"" << a.author << "\"\n"
        << "Price       : $" << a.price / 100.0 << endl
        << "Genre       : " <<a.genre << endl
        << "In stock? " << (a.status ? "yes" : "no") << endl
        << endl;
        return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code I been working on but I'm having a hard time
I'm having trouble getting the position of an address. However, this problem occurs only
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
I'm having problem selecting an element with an id like this <li =0f:Bactidol_Recorder.mp4>. I
My friend has this embarassing problem. He's trying to get the most basic of
Problem fixed. Thanks a lot! I am having the following error in the code
I'm having a problem which I easily solve with php and some queries, but
Dear Friends from Stackoverflow, Please help me with a problem that i'm having when
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
We're having problem with a huge number of legacy stored procedures at work. Do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.