I would like to define an ostream operator to let me easily output variables of type `alglib::complex`. To provide a working example without including the alglib library I'll instead overload the output of `complex<double>` below (this clarification because of an earlier version of the question). In the header file "my\_class.h" I have

```
using namespace std;
#include <complex>
#include <iostream>

class my_class {

    public:

    ostream& operator << (std::ostream& os, complex<double> a) {
        os << "(" << real(a) << "," << imag(a) << ")";
        return os;
    }

    void output(complex<double>);

    my_class() {}
    ~my_class() {}
};

```

And in the source file "my\_class.cpp" I have 

```
#include "my_class.h"

void my_class::output(complex<double> cd) {
    cout << cd << endl;
}

```

Finally I have a main method file "run\_my\_class.cpp":

```
#include "my_class.h"

int main(int argc, const char* argv[]) {

    my_class obj;
    complex<double> cd=complex<double>(1.0,-1.0);
    obj.output(cd);

}

```

I try to compile using

```
g++ -c my_class.cpp

```

but this gives me the error 

```
my_class.h:9:62: error: ‘std::ostream& my_class::operator<<(std::ostream&, std::complex<double>)’ must take exactly one argument
 ostream& operator << (std::ostream& os, complex<double> a) {

```

However, if I define the operator as a friend, namely `friend ostream& operator << (std::ostream& os, complex<double> a)`, it compiles and I compile the main method:

```
g++ run_my_class.cpp my_class.o -o run_my_class

```

And it works as it should. However this is not what it seems the `friend` keyword is for. Is there a better way to make this work?