vs2005中友元函数的问题
#include<iostream>template <class T>
class test
{
friend std::ostream & operator <<(std::ostream &out,test& t)
{
out<<t.x<<t.y;
return out;
}
public:
T x,y;
};
在这个程序里,如果把友元函数在类外定义就会出现连接错误,谁能解释一下??我用的是vs2005。
#include<iostream> using namespace std; template <class T> class test { friend ostream &operator << <T>(ostream &out,test<T>& t); public: T x,y; }; template <class T> ostream & operator << (ostream &out,test<T>& t) { out<<t.x<<t.y; return out; }
#include<iostream> using namespace std; template <class T> class test { template <class U> friend ostream &operator << (ostream &out,test<U>& t); public: T x,y; }; template <class T> ostream & operator << (ostream &out,test<T>& t) { out<<t.x<<t.y; return out; }