模板类的友元重载函数

#include

using namespace std;



template

class Test 

{

public:

friend ostream& operator << <> (ostream& out, const Test& obj);


Test(T n=0):num(n){}


Test(const Test& copy)

{

num = copy.num;

}


private:

T num;

};


template

ostream& operator << <> (ostream& out, const Test& obj)

{

out<

return out;

}


int main()

{

Test t(2);

cout<

return 0;

}


或者是


#include

using namespace std;



template

class Test 

{

public:

 template

friend ostream& operator << (ostream& out, const Test& obj);


Test(T n=0):num(n){}


Test(const Test& copy)

{

num = copy.num;

}


private:

T num;

};


template

ostream& operator << (ostream& out, const Test& obj)

{

out<

return out;

}


int main()

{

Test t(2);

cout<

return 0;

}

你可能感兴趣的:(C/C++)