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
You have to specify the function is friend of which class. You either put that function in the class declaration:
The other way is to declare it as a friend inside the class, and define it in some other place:
…