Question

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

Was it helpful?

Solution

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;
}

OTHER TIPS

Do you have the friend function prototyped inside the class? You need to have something inside the class indicating this is a friend function. Like the line

  friend ostream& operator<<(...);

or something. Look up a complete example for overloading the insertion/extraction operators for more information.

#include <iostream>
#include <string>
using namespace std;

class Samp
{
public:
    int ID;
    string strName; 
    friend std::ostream& operator<<(std::ostream &os, const Samp& obj);
};
 std::ostream& operator<<(std::ostream &os, const Samp& obj)
    {
        os << obj.ID<< “ ” << obj.strName;
        return os;
    }

int main()
{
   Samp obj, obj1;
    obj.ID = 100;
    obj.strName = "Hello";
    obj1=obj;
    cout << obj <<endl<< obj1;

} 

OUTPUT: 100 Hello 100 Hello Press any key to continue…

This can be a friend function only because the object is on the rhs of << operator and argument cout is on the lhs. So this cant be a member function to the class it can only be a friend function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top