This helped me with the exact same problem.
Solution:
Forward declare the friend
function before the definition of the class
itself. For example:
template<typename T> class MyClass; // pre-declare the template class itself
template<typename T> std::ostream& operator<< (std::ostream& o, const MyClass <T>& x);
Declare your friend function in your class with "<>" appended to the function name.
friend std::ostream& operator<< <> (std::ostream& o, const Foo<T>& x);