Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - must friend functions be defined in the header file?

I want to overload the operator << in one of my classes. The signature goes like this:

friend std::ostream& operator<<(std::ostream& os, const Annuaire& obj)

When I try to define it in the .cpp file it says that the operator<< exactly takes 1 argument, however, when I define it in the .h, it compiled/works fine.

This is how I define it in the .cpp file :

std::ostream& Annuaire::operator<<(std::ostream& os, const Annuaire& obj){ // ... }

Does it have anything to do with friend functions needing to be defined in header files?

like image 828
Pacane Avatar asked Dec 04 '11 05:12

Pacane


People also ask

Where should friend function be defined?

The friend function definition is found outside the class like a normal member function. The friend function is not defined using the friend keyword or use the scope resolution operator :: as it is not a member of the class in which it has been declared. A friend function can be declared in several classes.

What are the rules for defining friend function?

A friend function is a special function in C++ which in-spite of not being member function of a class has privilege to access private and protected data of a class. A friend function is a non member function or ordinary function of a class, which is declared as a friend using the keyword “friend” inside the class.

Can friend function be defined inside class?

Friend functions can be defined (given a function body) inside class declarations. These functions are inline functions. Like member inline functions, they behave as though they were defined immediately after all class members have been seen, but before the class scope is closed (at the end of the class declaration).

Which of the following is true about friend function?

Which of the following is correct about friend functions? Explanation: Friend function can be declared either in private or public part of the class. A friend function cannot access the members of the class directly. They use the dot membership operator with a member name.


2 Answers

It can be defined in a cpp file, but it needs to at least be declared in a header file, otherwise all places where you want to use it will only see the stuff the stream itself gives you, not your overload.

// .h and in class
friend std::ostream& operator<<(std::ostream& os, MyClass const& v);

// .cpp
std::ostream& operator<<(std::ostream& os, MyClass const& v){
    // print it
}
like image 50
Xeo Avatar answered Sep 29 '22 18:09

Xeo


The problem is with the way you're defining it. It's not a member of the class, it's just a friend of the class. You need to drop the Annuaire:: prefix. So, change this:

std::ostream& Annuaire::operator<<(std::ostream& os, const Annuaire& obj){ // ...

to this:

std::ostream& operator<<(std::ostream& os, const Annuaire& obj){ // ...

The reason for the error message is that Annuaire::operator<<(std::ostream& os, const Annuaire& obj) would expect three arguments: the Annuaire instance that it's called on (as this), and and two additional arguments (os and obj).

like image 42
ruakh Avatar answered Sep 29 '22 17:09

ruakh