+3  A: 

All the below said, if you don't need an operator to be a friend, then don't make it a friend. For output operators in particular, in my opinion you should not make them friends. That is because if your class can be output to a stream, it should have equivalent get functions that provide the same data programmatically. And in that event, you can write a operator<< as a non-friend in terms of those get functions.

In case you have some good reason for making them friends, you can do a friend definition

template <class A> class MyClass {
  public:
    friend ostream & operator<<(ostream & os, MyClass<A> const& mc) {
      // ...
    }
};

That way you don't need the template<...> clause that gets you the type A. It's alreay known if you define the operator inside the template. Note that even though you defined it inside the template, it's not a member function. It's still a non-member, but has access to the names declared in the class (like the template parameter). For each instance of MyClass you create, a different non-template operator function is created out of that friend function that prints things.

If you want to define the template outside, you have to predeclare it to be able to declare a given specialization of it as a friend.

// predeclare it so you can make it a friend.
template <class A> class MyClass;
template <class A> ostream &operator<<(ostream &os, MyClass<A> const&);

template <class A> class MyClass{
  public:
    /* the "<A>" is needed - it says that a given instantiation of
       that template is a friend, and not a non-template function. */
    friend ostream & operator<< <A>(ostream & os, MyClass<A> const& mc);
};

template <class A> 
ostream & operator<<(ostream & os, MyClass<A> const& mc){
  // some code
  return os;
}

That makes operator<< <Foo> a friend of MyClass<Foo>. If you were to omit the <A> or an also possible empty <>, the compiler would understand that as saying you made a non-template operator having concrete instead of templated parameters as friend.

The more easy but less "correct" solution is to make MyClass <Foo> have as friend all the operator << instantiations. So theoretically operator << <Bar> could access private members of MyClass <Foo>. It's not what is wanted, but it works too, gaining more access than needed. It gets rid of the need for forward declaring:

template <class A> class MyClass{
  public:
    /* make all instantiations friends. */
    template<typename T>
    friend ostream & operator<<(ostream & os, MyClass<T> const& mc);
};

template <class T> 
ostream & operator<<(ostream & os, MyClass<T> const& mc){
  // some code
  return os;
}
Johannes Schaub - litb
and could you please add, how does look like the signature of non-friend operator<< overload function? thank in advance...
coubeatczech