#include <iostream>
using namespace std;
template<class T>
class Test
{
public:
friend ostream& operator << <> (ostream& out, const Test<T>& obj);
Test(T n=0):num(n){}
Test(const Test<T>& copy)
{
num = copy.num;
}
private:
T num;
};
template<class T>
ostream& operator << <> (ostream& out, const Test<T>& obj)
{
out<<obj.num<<endl;
return out;
}
int main()
{
Test<int> t(2);
cout<<t;
return 0;
}
或者是
#include <iostream>
using namespace std;
template<class T>
class Test
{
public:
template<class T>
friend ostream& operator << (ostream& out, const Test<T>& obj);
Test(T n=0):num(n){}
Test(const Test<T>& copy)
{
num = copy.num;
}
private:
T num;
};
template<class T>
ostream& operator << (ostream& out, const Test<T>& obj)
{
out<<obj.num<<endl;
return out;
}
int main()
{
Test<int> t(2);
cout<<t;
return 0;
}