必须先“声明”类,再声明函数,然后再定义
template<class T> class A;template<class T> class B;template<class T> bool Foo(const A<T>& a, const T& n);
template<class T>
class A
{friend class B<T>; //不可以:friend class B<>;friend bool Foo<T>(const A& a, const T& n); //但可以:friend bool Foo<>(...);};
在普通函数中写:
class A
{template<class T> friend class B;template<class T> friend Foo(...);};
函数和B类的定义
template<class T>inline bool Foo(const A<T>& a, const T& n){return a.n==n;
}template<class T>class B
{};